Reputation: 25525
Information around referencing documents with this "string slash" notation is a little sparse. Here is an example provided in the docs:
constructor(private afs: AngularFirestore) {
this.userDoc = afs.doc<Item>('user/david');
...
}
I can tell it's referencing the user
collection and getting a doc with a unique id david
. But how can I determine the field for the unique index (in this case david
) that firestore looks up in that string-slash notation?
Upvotes: 0
Views: 256
Reputation: 25525
I didn't realize that the ID is implicitly set when you use doc()
and set()
in this way. In this case, david
was supplied as the id:
this.afs.doc(`user/david`).set(userObject);
So once you add it like that, it can be recalled by the ID you set. Calling that method over will replace (or "destructively set", as the docs note) what is stored under that ID.
Upvotes: 0
Reputation: 32604
David here!
1 + 2) Think of david
as the primary key in the users
collection.
3) You use a generated ID when you can ID is not important and you can get back the document with a query.
constructor(private afs: AngularFirestore): {
const shirtsCollection = afs.collection<Item>('tshirts', ref => {
return ref.where('price', '==', 10.00);
});
this.shirtsUnder10$ = shirtsCollection.valueChanges();
}
In some less common cases you an also create a lookup collection. Where you have one known id that contains a list of generated ids. This is useful for situations like "event attendees". If you know the ID of the event, you can get back a list of attendees IDs for the users who attended the event.
Upvotes: 2