Assuming we have a chat application that has a database structure that looks similar to this:
I know writing to the rooms collection would look like this:
await admin.firestore().collection("rooms").doc(roomA).set()
Would writing to the messages collection look like this?
await admin.firestore().collection("rooms").doc(roomA).collection(messages).doc(message1).set()
And just follow this structure all the way down, collection.doc.collection.doc.collection.doc...?
CodePudding user response:
Yes, that operation will a sub-document with ID message1
(you missed the quotes though). If you want to add a new document to sub-collection with a random ID, you can use add()
:
await admin.firestore().collection("rooms").doc('roomId').collection(messages).add({...data})
Alternatively, you can also write the path to that sub-document as:
await admin.firestore().doc('rooms/roomId/messages/messageId').set(...)