如何从Firebase参考中将孩子放在第一层?(浅查询) [英] How to get children on the first layer from a Firebase reference? (shallow query)
问题描述
我有一个Firebase数据库结构,如下所示:
I have a Firebase Database structure that looks like this:
/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, e.g.:
- abc001
- abc002
- abc003
...
- 我不知道该怎么做.据我所知,一个
value
事件也会为每个孩子(第二层和第三层)的每个孩子触发.
-
childAdded
事件也不是正确的选择,因为我只想接收一次数据(我不想监听数据更改/添加的子项,...).br> -
childAdded
的单事件仅触发一次并返回第一个子级(abc001
). - 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). - 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, ...). - A single-event of
childAdded
only triggers once and returns the first child (abc001
).
你能帮帮我吗?
谢谢.
推荐答案
这对于您的数据结构是不可能的.Firebase实时数据库没有浅查询".当您收听某个位置时,您将始终获得其所有嵌套数据.有没有计划对此进行更改.
This is not possible with your data structure. Firebase Realtime Database doesn't have "shallow queries". When you listen to a location, you get all of its nested data all of the time. There are no plans to change this.
您可以改为在数据库中创建一个仅包含要查找的数据的新位置.在NoSQL数据库中复制这样的数据是很常见的.您只需要记住,只要其中之一需要更改,就可以使所有重复项保持最新状态.
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. You just have to remember to keep all the duplicates up to date any time one of them needs to change.
这篇关于如何从Firebase参考中将孩子放在第一层?(浅查询)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!