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.
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,
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.