Skip to content Skip to sidebar Skip to footer

Is It Possible To Use Where In With A Collection Group Query In Firestore?

In my database: -Users(Top level collection): ---user1(doc): ----------pets(sub collection): --------------pet1(doc) --------------pet2(doc) ---user2(doc): ----------pets(sub colle

Solution 1:

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

Post a Comment for "Is It Possible To Use Where In With A Collection Group Query In Firestore?"