Reputation: 135
How would I go about adding a new document in Firebase/Cloud Firestore with a nested collection?
const createDocument = () => {
const documentName = prompt('Enter document name:');
if (documentName) {
db.collection('despacito').add({
name: documentName,
})
}
Any suggestions? Thank you for your time.
Upvotes: 0
Views: 2563
Reputation: 1247
Per the data model doc, Collections and documents are created implicitly in Cloud Firestore. Simply assign data to a document within a collection. If either the collection or document does not exist, Cloud Firestore creates it.
So no need to actually create a document with a nested collection.
Per the subcollections documentation to reference a new document in a subcollection you can reference it as:
const messageRef = db.collection('rooms').doc('roomA').collection('messages').doc('message1');
As you are using the .add method, it should be something like:
db.collection('despacito').doc('documentName').collection('subcollectionname').add({
name: nesteddocumentName,
})
Upvotes: 2