[英]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_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).我正在使用集合组查询来检索“item_A”下的所有订单(跨所有用户)。 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 (eg Paris).但现在我需要改进上述内容,以便能够过滤来自特定城市(例如巴黎)的订单。 So I tried adding a 'where' clause as follows:
所以我尝试添加一个“where”子句,如下所示:
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.
错误:[错误:[firestore/failed-precondition] 操作被拒绝,因为系统不在操作执行所需的 state 中。 Ensure your query has been indexed via the Firebase console.]
确保您的查询已通过 Firebase 控制台编入索引。]
I have already set up a composite index on my FireStore database with the following details:我已经在我的 FireStore 数据库上设置了一个复合索引,其中包含以下详细信息:
Collection ID = orders_item_A
集合 ID = orders_item_A
Fields indexed = address.city Ascending type Ascending
Fields indexed = address.city 升序类型 升序
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 ).
我想知道问题是否在于在“where”子句中使用 object 属性( 这应该不是问题)。 So I also tested with a simpler query such as:
所以我还用一个更简单的查询进行了测试,例如:
.where("type", "==", "A1")
But this too failed.但这也失败了。 What am I doing wrong?
我究竟做错了什么?
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 composite 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.注意:如果您碰巧删除了特定的“索引”,则需要一些时间(在某些情况下最多 10 分钟)才能准备好。 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.
所以请稍等,过一段时间再试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.