简体   繁体   English

如何使用 firebase 实时数据库获取当前用户尚未评论的所有帖子?

[英]How to get all posts where current user has not commented yet using firebase realtime database?

My use case is to fetch all posts which is not already commented by current user.我的用例是获取当前用户尚未评论的所有帖子。

I want to achieve this with single query but my current approach is not working.我想通过单个查询来实现这一点,但我目前的方法不起作用。

DB structure:数据库结构:

posts: {
   post1:{
      content: "Lorem Ipsum"
      comments: {
         uid1: true,
         uid2: true
                }
          }
         }

My query looks like this:我的查询如下所示:

   val postsRef = FirebaseDatabase.getInstance().reference.child("posts")
            .orderByChild("comments/{uid}")
            .equalTo(null)

Above query an index, but my current index is one dynamic value which firebase doesn't allow.上面查询一个索引,但我当前的索引是一个动态值,firebase 不允许。

"posts" : {
"$postId": {
  "comments": {
      ".indexOn" : ".value"
  }
}

} }

I'm stuck here, it would be helpful if anyone guide me to achieve this.我被困在这里,如果有人指导我实现这一目标,那将会很有帮助。 Thanks谢谢

Firebase database queries can only check for the presence of a certain value, not for the absence of it. Firebase 数据库查询只能检查某个值是否存在,不能检查是否存在。

So you can either add each user ID to each post and then remove them when they comment, or (more likely) you will have to implement the logic in your application without a database query: just load all posts and remove the ones where you then find that this user already commented.因此,您可以将每个用户 ID 添加到每个帖子,然后在他们评论时删除它们,或者(更有可能)您将不得不在没有数据库查询的情况下在您的应用程序中实现逻辑:只需加载所有帖子并删除您随后的帖子发现这个用户已经评论了。

暂无
暂无

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

相关问题 如何从 flutter 中的实时数据库中获取当前用户 - how to get current user from realtime database in flutter 如何使用规则禁止 Firebase 实时数据库中的用户? - How can I ban a User in Firebase realtime Database using the rules? 如何使用 Firebase 获取用户帖子并使用 Facebook 进行身份验证? - How to get user posts using Firebase and Authenticate Using Facebook? 一对多关系 Firebase 实时数据库(嵌入了数百万条评论) - One-To-Many Relationship Firebase Realtime Database (with miliions of commented embedded) 如何从一个用户的 firebase 实时数据库到 flutter 中的文本字段获取当前用户数据,如姓名和 email? - How to get current user data like name and email from firebase realtime databse for one user to Text Field in flutter? 如何使用 Firebase 9(模块化 sdk)更新 Firebase 实时数据库中的数据 - How to update data in Firebase realtime database using Firebase 9 (modular sdk) 如何通过 clearData() 和 user_privacy.json 路径删除 firebase 实时数据库中某些条件为真的整个条目? - How to delete entire entry in firebase realtime database where some condition is true, through clearData() and user_privacy.json path? 如何使用 Flutter 仅从 firebase 实时数据库中的给定数字获取时间范围? - How can I get only the time range from the given figure in firebase realtime database using Flutter? 如何从 Firebase 实时数据库中获取数据到 Flutter 的列表中? - How to get data from Firebase Realtime Database into list in Flutter? 如何查询firebase实时数据库获取上月数据 - How to query firebase realtime database to get last month data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM