Skip to content Skip to sidebar Skip to footer

Firebase Realtime Db: Howto Index Properly On Key-value Pair

Question I have this Firebase Realtime Database: { 'groupUsers' : { 'group1key' : { 'user1key' : 'admin' }, 'group2key' : { 'user1key' : 'admin',

Solution 1:

There is no way to pre-create the indexes for such a structure. I've covered this type of structure in my answer here and here and the new Firestore documentation (which works similarly in this aspect) even has a documentation page about it.

The recommendation is to store data in a way that allows this lookup in the other direction too. With your current structure you can efficiently look up the users in a group. To also allow looking up the groups for a user, add this structure:

{"usersGroups":{"user1key":{"group1key":"admin","group2key":"admin"},"user2key":{"group2key":"readonly",},"user3key":{"group2key":"readwrite"}}}

Post a Comment for "Firebase Realtime Db: Howto Index Properly On Key-value Pair"