[英]Firebase Database: get all child's data from childs
I want to be able to differentiate my content
between type
and get all other data but I don't know how to get past the IDs after content
.我希望能够区分我的
content
type
并获取所有其他数据,但我不知道如何在content
之后通过 ID。
I want every data from -NBC22CHEk3ebNxdlYOi, -NBC-Nfz6e5yppFlxU7S, -NBC1TVY-TzT7yfrSA3o and later be possible to get new video id's from existing and new users
我想要来自 -NBC22CHEk3ebNxdlYOi、-NBC-Nfz6e5yppFlxU7S、-NBC1TVY-TzT7yfrSA3o 的所有数据,以后可以从现有用户和新用户那里获得新的视频 ID
/content/{user_uid}/{content_uid}
/content/{user_uid}/{content_uid}
If I want only the content from one user then I can write this:
如果我只想要一个用户的内容,那么我可以这样写:
val uid = FirebaseAuth.getInstance().uid
// url is just a placeholder for the real database url
val ref = FirebaseDatabase.getInstance(url).getReference("/content/$uid")
I want all content with the type value "video" from all users.
我想要来自所有用户的类型值为“video”的所有内容。
That's actually not possible with your actual database structure.对于您的实际数据库结构,这实际上是不可能的。 All queries in the Realtime Database work on a flat list of nodes, where the value on which you want to perform the filter/order on, must be at a fixed path under each direct child node.
实时数据库中的所有查询都在一个平面节点列表上工作,您要对其执行过滤/排序的值必须位于每个直接子节点下的固定路径。
Unfortunately, that's not your case, because you have a dynamic key under the content
node, which is actually the UID, and which also means that you can't filter on type
.不幸的是,这不是你的情况,因为你在
content
节点下有一个动态键,它实际上是 UID,这也意味着你不能过滤type
。 If you want to allow that, then you have to create a flat list of videos, where the UID (SIH5...89j1) is a property inside each video object.如果您想允许这样做,那么您必须创建一个平面视频列表,其中 UID (SIH5...89j1) 是每个视频 object 中的一个属性。 This is already achieved since
publisher_uid
holds exactly that.这已经实现了,因为
publisher_uid
正是如此。
In short, remove the extra (UID) level from your database tree.简而言之,从数据库树中删除额外的 (UID) 级别。 Or, leave it as it is, and denormalize the data.
或者,保持原样,并对数据进行非规范化。 Basically create a new structure like this:
基本上创建一个这样的新结构:
/content/{content_uid}
See, there is no UID involved.看,没有涉及 UID。 If you're new to NoSQL databases, this might sound some kind of weird, but it's actually a quite common practice.
如果您是 NoSQL 数据库的新手,这听起来可能有些奇怪,但实际上这是一种很常见的做法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.