如何从Firebase数据库中检索多层多个子节点内的数据?

huangapple go评论95阅读模式
英文:

How to retrieve the data from Firebase Database inside muti-layer multiple child nodes?

问题

我一直在尝试从Firebase数据库中检索数据,但无法找到处理这种类型数据结构的方法。正如您在AllStores节点中所看到的,有多个商店,每个商店都有自己的数据和自己的产品,产品有许多主要类别,每个主要类别又有多个子类别,子类别有多个品牌,最后每个品牌都有多个带有随机编号的产品。我知道这个结构有点复杂,我可以通过分隔节点并一次又一次地创建来获取数据,但这有点繁琐。所以,有没有可能以单个对象的方式检索整个商店及其所有数据,并将其放入一个列表中,或者我是否必须手动放入路径,只需检索商店数据并使用reference.child("")方法来获取每个数据。如果我能找到一种在不改变节点的情况下检索这些数据结构的方法,那对我来说将会非常方便。非常感谢任何回答。
英文:

如何从Firebase数据库中检索多层多个子节点内的数据?

I have been trying to retrieve the data from Firebase Database but unable to find a way with this type of data structure. As you can see in the AllStores node, there are multiple stores and each store has its own data with own products and the products have many main categories and each main category has multiple sub category , sub- categories have multiple brands and finally each brand has multiple products with a random number as its node. I know this structure is a bit complicated and i can get the data by separating the nodes and create again and again which is a bit tedious. So, is the any possible way of retrieving the whole store with all of its data in an single object and put it on a list or do i have to manually put the path by just retrieving the store data and use reference.child("") method to get each and every data. If i find a way of retrieving this data structure without changing its nodes , it will be of great ease to me . Any answer is much appreciated.

答案1

得分: 1

> 是否有可能以单个对象的形式检索整个存储和其所有数据

是的,这已经是从实时数据库中获取节点时发生的情况。当您获取任何给定节点的DataSnapshot时,它将包含其中的所有子节点。您只需要在该快照上使用child(),以进入每个子节点,从而获得一个包含所有子数据的新DataSnapshot。以相同的方式继续深入每个子节点。每个子节点将嵌套在其他DataSnapshot中。

因此,如果您有一个“/AllStores/Mazbat Store”的DataSnapshot,您可以深入到您需要找到所需数据的快照位置。

DataSnapshot store = // 获取“/AllStores/Mazbat Store”的快照
DataSnapshot item = store.child("Store Products").child("Electronics").child("Camera").child("Asus").child("6432649");

显然,您需要编写大量代码来从顶层快照中获取所有内容,但所有内容都会在那里。

英文:

> is the any possible way of retrieving the whole store with all of its data in an single object

Yes, this is already what happens when you fetch a node from Realtime Database. When you get DataSnapshot for any given node, it will contain all of the child nodes inside it. All you have to do is use child() on that snapshot to dig into each of the child nodes to get a new DataSnapshot with all of the child data. Keep digging in to each child the same way. Everything child will be nested inside other DataSnapshots.

So, if you have a DataSnapshot of "/AllStores/Mazbat Store", you can go down as far as you need to find a snapshot with the data you want.

DataSnapshot store = // fetch of "/AllStores/Mazbat Store"
DataSnapshot item = store.child("Store Products").child("Electronics").child("Camera").child("Asus").("6432649")

You will obviously have to write a lot of code to get a hold of everything from the top level snapshot, but it will all be there.

huangapple
  • 本文由 发表于 2020年8月24日 05:28:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63552160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定