简体   繁体   English

Firebase实时数据库:4级嵌套上的orderByChild()。equalTo()

[英]Firebase Realtime Database: orderByChild().equalTo() on 4th level nesting

I'm trying to run a firebase realtime database query to filter data based on value nested at 4th level from root. 我正在尝试运行Firebase实时数据库查询,以根据嵌套在根目录第4级的值来过滤数据。 Below is the data structure and my query is: 下面是数据结构,我的查询是:

let ordersSnapshot = await admin.database().ref(`orders`).orderByChild(`info/infoStatus`)
            .equalTo("In Process")
            .once('value');

But this query is returning no data. 但是此查询未返回任何数据。 I have enabled indexing and there is no warning for it. 我已启用索引编制,并且没有警告。 I have also changed my query to .orderByChild('infoStatus') as well but no result. 我也将查询更改为.orderByChild('infoStatus') ,但没有结果。

If I set my ref one level below ie admin.database().ref('orders/userId1').orderByChild('info/infoStatus').equalTo("In Process") then it gets the result successfully. 如果我将我的裁判设置为低于admin.database().ref('orders/userId1').orderByChild('info/infoStatus').equalTo("In Process")则它将成功获取结果。 But in my scenario I don't have the user id which I can use in orderByChild . 但是在我的场景中,我没有可以在orderByChild使用的用户ID。

Is this the limitation of realtime database query ie I need to update my data structure or is there any error in my query ? 这是实时数据库查询的限制吗,即我需要更新数据结构,还是查询中有任何错误?

Orders Node: 订单节点:

{
    "userId1": {
        "orderId1": {
            "info": {
                "infoStatus": "In Process"
            }
        },
        "orderId2": {
            "info": {
                "infoStatus": "Complete"
            }
        }
    },
    "userId2": {
        "orderId1": {
            "info": {
                "infoStatus": "In Process"
            }
        }
    },
    "userId3": {
        "orderId1": {
            "info": {
                "infoStatus": "In Process"
            }
        }
    }


}

The property that you order/filter on must be at a fixed path under each child. 您订购/过滤的属性必须在每个子项下的固定路径上。 That means that in your current data structure, you can't filter on orders across all users, only on a specific user. 这意味着在您当前的数据结构中,您不能仅针对特定用户过滤所有用户的订单。

The typical solution is to flatten the data, storing the orders in a flat list, and adding a lookup list of order IDs to each user if that's still needed. 典型的解决方案是将数据展平,将订单存储在平面列表中,如果仍然需要,向每个用户添加订单ID的查找列表。 Something like: 就像是:

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"
        }
    }
}

Also see: Firebase Query Double Nested 另请参阅: Firebase双重嵌套查询

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

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