there is no documentation about how add sub collection in a document in firestore, how to add a sub collection by using a web app? even tried like this but didn't worked. how can i add a sub collection in apps by using code? is there any way to do it or it will be better to use firebase real time database? if it so, please let me know how to do it in firebase as welli want to add like this i did this but iam getting an error like this the error says this
A subcollection is a collection associated with a specific document. Note: You can query across subcollections with the same collection ID by using Collection Group Queries. You can create a subcollection called messages for every room document in your rooms collection: collections_bookmark rooms.
You just need to get a reference to the subcollection, which you get from the DocumentReference . And on that you simply use the regular method for writing data again: firebase.google.com/docs/firestore/manage-data/add-data. If you're having a hard time making this work, update your question to show what you've tried.
I’d like to talk at a high level about Firestore sub-collections. First, we need to review the Firestore data model. Firestore is a document/collection database. Documents hold whatever JSON data you’d like, but they must live within a collection. A document can have any number of sub-collections with their own documents.
Set the data of a document within a collection, explicitly specifying a document identifier. Add a new document to a collection. In this case, Cloud Firestore automatically generates the document identifier. Create an empty document with an automatically generated identifier, and assign data to it later.
If you want to create messages collection and call addDocument () 1000 times will be expensive for sure, but this is how Firestore works. You can switch to Firebase if you want, where the number of writes doesn't matter.
You can see these orphaned sub-collections by clicking through the Firestore web console. But you have to click through every single document to find them, because orphaned sub-collections don’t show up when you query their parents. This is garbage.
Your code should work if you change .set
to .add
. Cloud Firestore will then issue a new ID for that document. If you want to specify the document ID, then you'll need to do this...
db.collection('users').doc(this.username).collection('booksList').doc(myBookId).set({
password: this.password,
name: this.name,
rollno: this.rollno
})
This page details how to Add data to Cloud Firestore
db.collection('users').doc(this.username).collection('booksList').add({
password: this.password,
name: this.name,
rollno: this.rollno
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With