简体   繁体   English

Firebase 数据库:从孩子那里获取所有孩子的数据

[英]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.

相关问题 将 Firebase 数据库中的所有数据导入 Mailchimp - Import all data from Firebase Database into Mailchimp 如何在android studio-java的firebase数据库中搜索得到子节点的具体数据? - How to get specific data of child node by searching from firebase database in android studio - java? 如何从 firebase 实时数据库中获取不同孩子的值? - How to get values from different child in firebase realtime database? 如何从 Swift 和 Firebase 中的子节点获取数据数组? - How to get array of data from a child node in Swift and Firebase? 如何将孩子的孩子与本地存储中的数据匹配(Firebase 实时数据库) - How to Match Child of a child with data from local storage (Firebase Realtime Database) Firebase:如何获取所有子节点的列表? - Firebase: How to get a list of all child nodes? 从 Firebase 数据库节点读取一些数据(不是全部) - Reading some of data (not all) from a Firebase database node 如何从 firebase 中的集合中的所有文档中获取特定数据? - How to get specific data from all documents from a collection in firebase? 如何从 Firebase 实时数据库中获取数据到 Flutter 的列表中? - How to get data from Firebase Realtime Database into list in Flutter? 从 firebase 数据库 flutter 中的 uid 获取用户数据 - get user data from uid in firebase database flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM