简体   繁体   中英

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.

"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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM