最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Filter a collection group query in FireStore - Stack Overflow

programmeradmin2浏览0评论

I have a database structure as follows (simplified for purposes of this question):

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 using a collection group query to retrieve all the orders under "item_A" (across all users). I am able to get this to work via:

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());
        });
    })

But now I need to refine the above to be able to filter for orders from specific cities (e.g. Paris). So I tried adding a 'where' clause as follows:

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());
        });
    })

But this fails, and I get the following 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 set up a posite index on my FireStore database with the following details:

Collection ID = orders_item_A

Fields indexed = address.city Ascending type Ascending

Status = Enabled

I am not sure what I am doing incorrectly. I wondered if the problem is with using an object property within the 'where' clause (which should not be the problem). So I also tested with a simpler query such as:

.where("type", "==", "A1")

But this too failed. What am I doing wrong?

I have a database structure as follows (simplified for purposes of this question):

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 using a collection group query to retrieve all the orders under "item_A" (across all users). I am able to get this to work via:

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());
        });
    })

But now I need to refine the above to be able to filter for orders from specific cities (e.g. Paris). So I tried adding a 'where' clause as follows:

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());
        });
    })

But this fails, and I get the following 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 set up a posite index on my FireStore database with the following details:

Collection ID = orders_item_A

Fields indexed = address.city Ascending type Ascending

Status = Enabled

I am not sure what I am doing incorrectly. I wondered if the problem is with using an object property within the 'where' clause (which should not be the problem). So I also tested with a simpler query such as:

.where("type", "==", "A1")

But this too failed. What am I doing wrong?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jun 2, 2020 at 17:25 Shailesh AppukuttanShailesh Appukuttan 83210 silver badges22 bronze badges 5
  • Are you sure that the value of type is "item_A"? Also, check the android native log, you will get a link to create the index directly which for wherever reason doesn't show up on RN. – Aawaz Gyawali Commented Jun 2, 2020 at 17:29
  • Your query is using a dynamic collection group name and we can't see the value of type. Please edit the code to hard code the collection group name, so we can all be certain that your index matches your query. – Doug Stevenson Commented Jun 2, 2020 at 17:34
  • @AawazGyawali: Not sure I understood. The value of "type" (in the above example) will be either "A1" or "A2". You are right, RN doesn't show the link for creating the index. I tried doing "adb logcat" but found the exact same error message as posted in the question. Is there another way to find the link to create the index? – Shailesh Appukuttan Commented Jun 2, 2020 at 17:37
  • @DougStevenson: Oops, that was a typo . I have corrected it in the question now. (Also had tested with hard-coded values in my queries, but had the same results). – Shailesh Appukuttan Commented Jun 2, 2020 at 17:40
  • Also... not sure I understood what you meant by "and we can't see the value of type". It is either "A1" or "A2" in the above database snapshot. Or were you asking something else? – Shailesh Appukuttan Commented Jun 2, 2020 at 17:46
Add a ment  | 

1 Answer 1

Reset to default 8

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:

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

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

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 = [];
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());
        });
    })

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.

发布评论

评论列表(0)

  1. 暂无评论