0

I want to .indexOn userId which is not static.

My realtime database structure looks like this:

posts: {
  content: "Lorem Ipsum"
  likes: 26,
  comments: {
     userId1: true,
     userId2: true
  }
}

I want to get all the posts where current user not commented yet. My Query looks like this.

val postsDatabaseRef = FirebaseDatabase.getInstance().reference.child("posts")
            .orderByChild("comments/${currrentUserId}")
            .equalTo(null)

I'm getting this warning:

Index not defined, add ".indexOn": "comments/aMrYUjIZiDNfSdqXnXYzJPdIEtq2", for path "/posts", to the rules

This is not working out,index warning is not gone.

    "posts" : {
    "$postId": {
      "comments": {
          ".indexOn" : ".value"
      }
    }
  }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Fortray
  • 133
  • 1
  • 1
  • 8
  • The structure you have makes it easy to find the commenters for a specific post. It does not however makes it easy to find the posts that a specific user commented on. To allow that, add an additional data structure where you map from UID back to the post IDs they commented on. `userComments: { uid1: { post1: true, post2: true }, uid2: { post1: true, post3: true } }`. For more on this, see the questions I linked. – Frank van Puffelen Apr 19 '22 at 04:01
  • Hi @FrankvanPuffelen, My use-case is only get all the posts user has not commented yet. Can we do this with any approach? – Fortray Apr 19 '22 at 04:25
  • Since you posted that as a separate question, I answered there: https://stackoverflow.com/questions/71919847/how-to-get-all-posts-where-current-user-has-not-commented-yet-using-firebase-rea – Frank van Puffelen Apr 19 '22 at 14:27

0 Answers0