Firebase查询具有条件的嵌套子项的所有用户(Nodejs) [英] Firebase Query all users with nested childs , with conditions (Nodejs)
问题描述
如何进行查询以创建具有此结构的所有用户的报告.我阅读了文档,但是似乎我无法真正进行此类查询,例如:我想查询所有拥有活动动物保险的用户.
How can I make query to create reports to all users with this structure. I read the documentations, but it seems I can't really make those kinds of queries, for eg: I want to query all users that have insurances of animals that are active.
users
uuid
...
insurances -> Animals
1 -> state:active
date:...
2 -> state:requested
date:...
...
推荐答案
Firebase查询在您查询的位置下的子节点平面列表上进行操作.他们只能对每个直接子项下已知路径上的值进行排序/过滤.
Firebase queries operate on a flat list of child nodes under the location you query. They can only order/filter on values that are at a known path under each direct child.
由于您的值在insurances/Animals/???
下,因此无法查询这些值.
Since your values are under insurances/Animals/???
, it can't query those values.
您通常需要通过以下方法之一解决此问题:
You'll typically need to solve this by one of these:
- 为查询创建所有动物的平面列表.此列表中的每只动物都必须至少具有要过滤的值,但可以是每个用户下动物信息的完整副本.
- 为每个用户提供一个属性,该属性指示他们是否有被保险的动物,或者他们拥有被保险的动物的数量,并在您更新保险时对其进行更新.这样,您可以使用
firebase.database().ref("users").orderByChild("insuredAnimalCount").startAt(1)
进行查询.
- Creating a flat list of all animals for the query. Each animal in this list must have at least the value you want to filter on, but can be a complete duplicate of the animal information under each user.
- Giving each user a property that indicates whether they have any insured animal, or the number of insured animals they have, and update that when you update the insurances. That way you can query with
firebase.database().ref("users").orderByChild("insuredAnimalCount").startAt(1)
.
也请在这里查看我的答案: Firebase Query Double Nested
Also see my answer here: Firebase Query Double Nested
这篇关于Firebase查询具有条件的嵌套子项的所有用户(Nodejs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!