How to Perform Collection Group Query Using Document Id in Cloud Firestore

Flutter-Firestore: get Document with know id from Collection Group Query

You can use collectionGroup() query with a where() clause.

Firestore.instance.collectionGroup('users').where('userIdField', isEqualTo: 'USER_ID')

You can read the user's company ID from the DocumentReference of that fetched document.

As @Frank commented, using where with document ID in a collection group query will not match. You would have to store user's UID as a field as well.

Firestore collection group query on document id

Adding the uid to the document itself is the only possible way at the moment and then query on that field:

this.db.firestore
.collectionGroup('members')
.orderBy('dateModified', 'desc')
.where("uid", '==', uid)

There was a Github issue for the same and explains why that's not possible.

That's pretty much why I sometimes prefer to store a root level collection members. Each document in the collection will have contain the groupID (or whatever your parent collection is meant for). If you use userID as the key for documents in there then it goes easy.

this.db.firestore.collection("members").doc(uid)

So instead of having a path like: /groups/{groupID}/members/{memberID}, the structure will be like: /groups/{groupID} and all the members will be store in the root level collection 'members'. A document in that collection may look like:

// uid as doc key
{
groupId: "groupID",
...otherFields
}

The catch is if a member can join multiple groups you cannot use the userId as the key.

Firestore collection group query on documentId

There is no way you can use the following query:

db.collectionGroup('likedBy').where(firebase.firestore.FieldPath.documentId(), '==', "User A").get();

And this is because collection group queries work only on document properties and not on document ids. According to the official documentation regarding collection group queries:

db.collectionGroup('landmarks').where('type', '==', 'museum');

You query the landmarks subcollection where the type property holds the value of museum.

A workaround could be to store the id of the user in an array and use array-contains but remember, for each collection group query you use, you need an index and unfortunately you cannot create such an index programmatically. Even if you can create an index in the Firebase console, it won't help you since you get those ids dynamically. So is not an option to create an index for each user separately because you'll reach the maximim number of indexes very quickly.

Maximum number of composite indexes for a database: 200

To solve this kind of problems, you should consider adding an array under each user object and use a query like this:

usersRef.where("usersWhoLikedMe", "array-contains", "someUserId")

Where usersWhoLikedMe is a property of type array.

Is it possible to use Where In with a Collection Group query in Firestore?

A collection group query is the only place where a filter on FieldPath.documentId() does not work the way you expect. That's because of some details about the way that this token actually works. If you try to this anyway, you will get an error like this:

Invalid query. When querying a collection group by FieldPath.documentId(), the value provided must result in a valid document path, but 'x' is not because it has an odd number of segments.

If you want to do a filter on document IDs in a collection group query, you will need to store the ID of the document as the value of a field in each document. If you use the field called "id", then you can filter on that field like this:

firestore
.collectionGruop("pets")
.whereIn('id', list)

This will give you a different error saying that you need to create an index, and give you a link to do so. After you create that index (it might take some time), you should be good to go.

See also: How to perform collection group query using document ID in Cloud Firestore

Firebase Collection Group Query on Id / Key

You can solve this with the help of FieldPath.documentId() like in the following line of code:

db.collectionGroup('teams').where(firebase.firestore.FieldPath.documentId(), '==', 'teams/teamId').get()

This query will return all documents with a specific team id.

firestore query document that have a collection contains a document

Firestore queries can only order/filter on fields in the documents that they return. There is no way to filter on fields in other documents, or the existence of other documents.

The common way to deal with this is to have a field in the parent document that indicates whether the child document you're looking for exists, and updated that on every relevant write. Then you can use that field to filter the documents in collection A.



Related Topics



Leave a reply



Submit