Skip to content Skip to sidebar Skip to footer

Filter A Collection Group Query In Firestore

I have a database structure as follows (simplified for purposes of this question): Collection: item_A -> Document: params = {someParameter: 'value'} -> Document: user

Solution 1:

I figured out the problem. My mistake was apparently in having made a "Composite" index (as I mentioned in the question) as it was the opening page on clicking "Indexes". I should have made a "Single field" index:

enter image description here

After deleting my existing composite index, I made a new "single field" index with details as:

enter image description here

Click 'Next' and on the next screen, "enable" one of the options under 'Collection group scope' (I chose Ascending):

enter image description here

It is important to do the above, or else the query will not work. And then everything worked as expected, with my original code:

let orders = [];
awaitfirestore()
    .collectionGroup("orders_item_A")
    .where("address.city", "==", "Paris")
    .get()
    .then(function (querySnapshot) {
        querySnapshot.forEach(function (doc) {
            console.log(doc.id, ' => ', doc.data());
            orders.push(doc.data());
        });
    })

Note: If you happen to delete a particular "index", it takes some time (upto 10 minutes on some occasions) to be ready. During this period you will get an error if trying to create an index for the same entry. So wait, and try again after some time.

Post a Comment for "Filter A Collection Group Query In Firestore"