[英]Firebase Realtime Database - orderByChild().equalTo() does not return expected node
[英]Firebase Realtime Database: orderByChild().equalTo() on 4th level nesting
我正在尝试运行Firebase实时数据库查询,以根据嵌套在根目录第4级的值来过滤数据。 下面是数据结构,我的查询是:
let ordersSnapshot = await admin.database().ref(`orders`).orderByChild(`info/infoStatus`)
.equalTo("In Process")
.once('value');
但是此查询未返回任何数据。 我已启用索引编制,并且没有警告。 我也将查询更改为.orderByChild('infoStatus')
,但没有结果。
如果我将我的裁判设置为低于admin.database().ref('orders/userId1').orderByChild('info/infoStatus').equalTo("In Process")
则它将成功获取结果。 但是在我的场景中,我没有可以在orderByChild
使用的用户ID。
这是实时数据库查询的限制吗,即我需要更新数据结构,还是查询中有任何错误?
订单节点:
{
"userId1": {
"orderId1": {
"info": {
"infoStatus": "In Process"
}
},
"orderId2": {
"info": {
"infoStatus": "Complete"
}
}
},
"userId2": {
"orderId1": {
"info": {
"infoStatus": "In Process"
}
}
},
"userId3": {
"orderId1": {
"info": {
"infoStatus": "In Process"
}
}
}
}
您订购/过滤的属性必须在每个子项下的固定路径上。 这意味着在您当前的数据结构中,您不能仅针对特定用户过滤所有用户的订单。
典型的解决方案是将数据展平,将订单存储在平面列表中,如果仍然需要,向每个用户添加订单ID的查找列表。 就像是:
users: {
"userId1": {
"orderId1": true,
"orderId2": true
},
"userId2": {
"orderId3": true
}
"userId3": {
"orderId4": true
}
}
orders: {
"orderId1": {
"info": {
"infoStatus": "In Process"
}
},
"orderId2": {
"info": {
"infoStatus": "Complete"
}
},
"orderId3": {
"info": {
"infoStatus": "In Process"
}
},
"orderId4": {
"info": {
"infoStatus": "In Process"
}
}
}
另请参阅: Firebase双重嵌套查询
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.