0

This is what my database looks like..

{
"users":{
     "user_id4243":{
        "friends":{
             "friend1_ID5321":1,
             "friend2_ID5321":1,
             "friend3_ID5321":1}
        "sData":"some_data"
     }
   }
}

In the other words, I store user's friend's Id's, and try to filter users, using a friend ID.

Everything works perfectly, except it downloads the whole "friends" data, and does the filtering on the client side. I get logs saying(Android/iOS),

Consider adding '".indexOn": "friends/115883230405231926879"' at users to your security and Firebase Database rules...

But how should I do that with no static user ids? I've tried the following method, but it doesn't work as indexOn required only static path..

"users": {
      "$user_id": {
          "friends": {
                ".indexOn": "$user_id"
            }
        }
    }

I've even tried adding another "ID" variable inside the friend id, but still no success..

"users": {
      "$user_id": {
          "friends": {
              "$user_id": {
                ".indexOn": ["ID"]
              }
            }
        }
    }

Any idea how can I use .indexOn in this situation? Or how can I make a structure, where I'll be able to filter users, that will have certain ID inside..?

Henrik
  • 339
  • 3
  • 13
  • You can't index on a dynamic path like that. You data structure makes it easy to find the friends for a user, but not the users for a friends. To allow that you'll want to add an additional data structure with the reverse mapping, commonly referred to as an inverted index. See https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Dec 20 '22 at 19:54

0 Answers0