1
votes

I’m quite new to firestore/ionic so apologies in advance if this question is silly.


I’m working on an Ionic3 project using firestore for my database and was wondering, how do you create an empty document (i.e. a document with an id, but no other fields/values)? I’ve been able to do this by hardcoding through the firestore console but I was wondering how to do it from inside my code?

For some context, I have a ‘classes’ collection and each class’s document needs to contain multiple student IDs to indicate which students are enrolled in the class. I only need the IDs to be stored (no other fields).

Currently, I’ve been doing this like this:

this.afs.collection('classes').doc(data).collection('students').add({studentId: this.userId});


but this creates an unnecessary auto-generated ID.

Is there any way to create a document with just an ID field, and if not, how else might I be able to do this?

Thanks in advance!

1
Why don't you store your students in a array? Firestore does have array type. - Duannx
Yeah, that works a lot better, don't know why I hadn't thought of it before! Thank you! - dusterquill

1 Answers

0
votes

If data is just a string, then the code you shared will (as far as I know) not create a document under classes, but only create the students sub-collection.

The Firebase console will show the data "document" in italic to indicate that there is no actual data for this document, and that it is just used to give you something to click on to see the sub-collection(s).