Angularfire2 Firestore文档斜杠引用符号及唯一索引字段疑问
Let me break this down clearly for you—this is a common point of confusion when first working with Firestore!
What does 'user/david' actually represent?
That slash-separated string is a full document path in Firestore, following a strict, built-in format:
[collection-name]/[document-ID]
In your example code:
user= the name of your top-level Firestore collectiondavid= the unique ID of the specific document inside that collection
The "unique index field" here is the document ID itself
Firestore automatically treats each document's ID as its unique identifier within its parent collection. You don’t need to create any custom index or add a special field for this—Firestore maintains an implicit, built-in index for document IDs by default.
When you call afs.doc<Item>('user/david'), Firestore isn’t running a query against a field like name or userId; it’s doing a direct lookup for the document in the user collection that has the exact ID david. This is the fastest way to fetch a single document because it’s a direct reference, not a filtered search.
A quick note on nested paths
If you’re working with subcollections, the path just extends with the same pattern:
[parent-collection]/[parent-document-ID]/[subcollection-name]/[subdocument-ID]
For example: 'users/david/posts/my-first-post' would point to a document named my-first-post in the posts subcollection of the david document in the users collection.
Key difference from field-based queries
It’s important to distinguish this from querying by a custom field. If you wrote something like:
this.userCol = afs.collection<Item>('user', ref => ref.where('username', '==', 'david'));
That would filter documents based on a custom username field, which might require a custom index. But the doc() method skips all that—it’s a direct pointer using the document’s inherent unique ID.
内容的提问来源于stack exchange,提问作者inorganik

