Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add sub collection to a document in firestore? [closed]

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

like image 244
user8167002 Avatar asked Feb 19 '18 20:02

user8167002


People also ask

What is sub collection firestore?

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.

How do I update Subcollection in firestore?

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.

What are FireStore sub-collections and how do they work?

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.

How do I assign data to a document in Cloud Firestore?

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.

How many times can adddocument () be called in FireStore?

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.

How do I see orphaned collections in FireStore?

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.


1 Answers

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

Set a book with a given ID

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

Add a book

db.collection('users').doc(this.username).collection('booksList').add({
  password: this.password,
  name: this.name,
  rollno: this.rollno
})
like image 167
Jason Berryman Avatar answered Sep 24 '22 10:09

Jason Berryman