I have a doc tree which looks like:
connections(collection):
connection1(docId):
...fields...
intentItems(subcollection):
intentItem1(docId):
...fields...
intentItem2(docId):
...fields...
connection2(docId):
...fields...
intentItems(subcollection):
intentItem3(docId):
...fields...
intentItem4(docId):
...fields...
As an admin, I want to read an intentItem
by id without knowing its connection
id.
I tried;
import {doc, documentId, FieldPath} from "@firebase/firestore";
import {query, where} from "firebase/firestore";
//FirebaseError: Invalid query. When querying a collection group by documentId(), the value provided must result in a valid document path, but 'XXXX' is not because it has an odd number of segments (1).
query(collectionGroup(db, Collections.intentItems), where(documentId(), "==", term))
//this returns with empty (** used as wildcard in rules)
useDocument(doc(db, Collections.connections, '**', Collections.intentItems, term))
Do I really need to index an id
field in the intentItem
s if I want to get them back as a result?
CodePudding user response:
Each entry in a Firestore index has to be unique. For indexes on a single collection, it automatically adds the document ID to ensure this. But in a collection group index, multiple documents might have the same ID. That's why, in a collection group index Firestore actually stores the entire document path instead of just the document ID.
o that means that this code won't work:
where(documentId(), "==", term)
As a workaround I recommend storing the document ID in the document itself too, creating a separate index with that value in it, and then filtering on that field rather than the built in documentId()
.