简体   繁体   English

Firebase 从孙子密钥中获取祖父母 object

[英]Firebase get grandparent object from grandchild key

I try to retrieve all the grand parent objects when a grandchild key is matched.当孙键匹配时,我尝试检索所有的祖父对象。 In the Firebase realtime database below, I have for example the sensor key "-LNVBiM1SGoAFvlMfi1V", and I need to retrieve with it all the orders objects where this key is found (04TBR7 in this example).在下面的 Firebase 实时数据库中,我有例如传感器键“-LNVBiM1SGoAFvlMfi1V”,我需要用它检索找到该键的所有订单对象(本例中为 04TBR7)。

I know the database structure might not be perfect but there is no possibility to modify it for now.我知道数据库结构可能并不完美,但目前无法修改它。

I tried the following code but it is not right, I guess the equalTo is not working since it is an Object:我尝试了以下代码,但它不正确,我猜equalTo不起作用,因为它是 Object:

db.ref('/orders').orderByChild('sensors').equalTo(sensor_id).once('value', snapshot => {
// my snapshot is always empty ...
});

在此处输入图像描述 在此处输入图像描述

There is no way to use orderByChild("sensors") in the way you're trying, as it checks the value of the sensors property (not the key).无法按照您尝试的方式使用orderByChild("sensors") ,因为它会检查sensors属性(而不是键)的

It's important to keep in mind that Firebase database queries work on a flat list of nodes.重要的是要记住 Firebase 数据库查询在节点的平面列表上工作。 So you can search for a specific order property across all orders, and you can search sensors under a specific order.因此,您可以在所有订单中搜索特定订单属性,也可以搜索特定订单下的传感器。 But you can not search across all sensors for all orders.但是您无法在所有传感器中搜索所有订单。

Since you say you can't modify the data structure, that unfortunately means you'll have to load the entire JSON and find the matching nodes client-side.由于您说您不能修改数据结构,不幸的是,这意味着您必须加载整个 JSON 并在客户端找到匹配的节点。

Also see:另见:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM