My database structure is set up like this (simplified version):
Collection: item_A
-> Document: params = {someParameter: "value"}
-> Document: user_01
-> Sub-collection: orders_item_A
-> Document: order_AA = {type: "A1", address: {pincode: "000000", city:"Paris"}
-> Document: order_AB = {type: "A2", address: {pincode: "111111", city:"London"}
...
-> Document: user_02
-> Sub-collection: orders_item_A
-> Document: order_AC = {type: "A1", address: {pincode: "222222", city:"Berlin"}
-> Document: order_AD = {type: "A1", address: {pincode: "333333", city:"Paris"}
...
I am currently using a collection group query to fetch all orders under "item_A" (across all users). This is how I achieve this:
let orders = [];
await firestore()
.collectionGroup("orders_item_A")
.get()
.then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
console.log(doc.id, ' => ', doc.data());
orders.push(doc.data());
});
})
Now, I want to narrow down the above query to filter for orders from specific cities (e.g. Paris). I attempted to add a 'where' clause in this manner:
let orders = [];
await firestore()
.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());
});
})
However, this approach failed and I received the following error message:
Error: [Error: [firestore/failed-precondition] Operation was rejected because the system is not in a state required for the operation's execution. Ensure your query has been indexed via the Firebase console.]
I have already created a composite index in my FireStore database with the following specifications:
Collection ID = orders_item_A
Fields indexed = address.city Ascending type Ascending
Status = Enabled
I am uncertain about what I might be doing incorrectly. I considered if the issue lies with using an object property within the 'where' clause (which should not be the problem). Therefore, I also experimented with a simpler query like this:
.where("type", "==", "A1")
Nevertheless, this attempt also failed. Where am I going wrong?