简体   繁体   English

如何从Firebase参考中将孩子放在第一层? (浅查询)

[英]How to get children on the first layer from a Firebase reference? (shallow query)

I have a Firebase Database structure that looks like this: 我有一个Firebase数据库结构,如下所示:

/users
    - abc001
        - name: "Harry Watson"
        - credentials: 
            - email: "h.watson@yahoo.com"
    - abc002
        - name: "Jason Polakow"
            - credentials: 
                - email: "j.polakow_1@gmail.com"
    - abc003
        - name: "Madeleine Février"
        - credentials: 
            - email: "mad_fev@gmail.fr"
    - ...

I would like to get all children on the first layer, eg: 我想让所有孩子都在第一层,例如:

- abc001
- abc002
- abc003
...
  • I don't know how to do this. 我不知道该怎么做。 As far as I know, a value event would also trigger for every child of every child (second and third layer). 据我所知, value事件也将为每个孩子的每个孩子(第二层和第三层)触发。
  • A childAdded event is also not the right choice, because I only want to receive the data once (I don't want to listen for data changes/child added, ...). childAdded事件也不是正确的选择,因为我只想接收一次数据(我不想监听数据更改/添加的子项,...)。
  • A single-event of childAdded only triggers once and returns the first child ( abc001 ). childAdded单事件仅触发一次并返回第一个子对象( abc001 )。

Could you help me out? 你能帮我吗?

Thanks in advance. 提前致谢。

This is not possible with your data structure. 您的数据结构无法做到这一点。 Firebase Realtime Database doesn't have "shallow queries". Firebase实时数据库没有“浅查询”。 When you listen to a location, you get all of its nested data all of the time. 收听某个位置时,您始终会获取其所有嵌套数据。 There are no plans to change this. 没有计划改变这一点。

You could instead create a new location in your database that contains just the data you're looking for. 您可以改为在数据库中创建一个仅包含您要查找的数据的新位置。 It's common to duplicate data like this in NoSQL databases. 在NoSQL数据库中复制这样的数据是很常见的。 You just have to remember to keep all the duplicates up to date any time one of them needs to change. 您只需要记住,只要其中之一需要更改,就可以使所有重复项保持最新。

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

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