Firebase Realtime DB: Howto index properly on key-value pair

前端 未结 1 863
野性不改
野性不改 2021-01-16 18:27

Question

I have this Firebase Realtime Database:

{      
  "groupUsers" : {
    "group1key" : {
      "user1key" : &quo         


        
相关标签:
1条回答
  • 2021-01-16 19:08

    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"
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题