How to create a collection in collection like this:
firestore().collection('users').doc(user.uid).collection("account").doc('doc-account').set({...});
How to do it ?
CodePudding user response:
Your question is about how to create a subcollection named account
under a root collection named users
.
You just need to do exactly the way you have shown in your question, using the set()
method on the doc-account
DocumentReference
: as soon a first document will be added under the account
subcollection, this subcollection will exist.
Same effect if you use the add()
method on the account
subcollection, as follows:
firestore().collection('users').doc(user.uid).collection("account").add({...});
And you even don't need to have a users
collection, i.e. having a document in the users
collection. This last point is a bit surprising at first sight, let's explain it with an example:
Take a doc1
document under the col1
collection
col1/doc1/
and another one subDoc1
under the subCol1
(sub-)collection
col1/doc1/subCol1/subDoc1
Actually, from a technical perspective, these two documents are not at all relating to each other. They just share a part of their path but nothing else. This is why the subDoc1
document (and its parent subCol1
collection) can exist without the doc1
existing. One notable side effect of this is that if you delete a document, its sub-collection(s) still exist.