Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
In a Cloud Function, what syntax is used for writing to a Firestore sub-collection?

Assuming we have a chat application that has a database structure that looks similar to this:

enter image description here

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...?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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(...)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!