58

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

user8167002
  • 701
  • 1
  • 5
  • 6
  • 2
    The solution goes: Get the interested document (doc) and then: doc.ref.collection('Collection_Name').add('Object_to_be_added') It is advisable to include a then/catch after the promise. – Santiago M. Quintero May 28 '18 at 19:22
  • 1
    OP can you please put code blocks into your question, and not images of code? – dwjohnston Apr 12 '19 at 01:56

1 Answers1

84

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
})
Jason Berryman
  • 4,760
  • 1
  • 26
  • 42