0

Lets say that I have a node for users in my database, to which the following rules apply:

{
  "users" : {
    "$user" : {
      ".indexOn" : "username" 
    } 
  } 
}

I know that you can build a query to get a user named Foo like this: firebase.database().ref('users').orderByChild('username').equalTo('Foo').once(...)

But like this I still have to load the whole tree node. Is it possible to get the value I specified in .indexOn only?

King Natsu
  • 461
  • 4
  • 19
  • 1
    No, it isn't possible to get a single key and value like that. If you want to optimize for accessing just the username, you'd create another top-level key, called (for example) `"usernames"` which would contain key value pairs where the key is `$user` and the value is their username. Then make sure to update both the `"users"` and `"usernames"` when changing a username. https://firebase.google.com/docs/database/web/structure-data#fanout – Sidney Oct 04 '17 at 23:32
  • 1
    Since I just answered the same elsewhere, have a look at: https://stackoverflow.com/questions/46562214/firebase-rules-to-read-specific-leaf-child-node-from-parent-nodes?noredirect=1#comment80108243_46562214 or an older one here: https://stackoverflow.com/questions/31305915/more-efficient-way-to-retrieve-firebase-data/31307494#31307494 – Frank van Puffelen Oct 05 '17 at 04:25

0 Answers0