Adding and “.indexOn”: “.value” into Firebase Rules

后端 未结 2 517
遇见更好的自我
遇见更好的自我 2021-02-07 12:25

I\'m studying AngularJS and Firebase and I\'m playing with a simple ChatApp, just to understand the code.

My Firebase DB structure is like this:

\"chat\"         


        
相关标签:
2条回答
  • 2021-02-07 13:00

    To index this structure to allow efficient querying, you have to add an index for each user:

    {
        "rules": {
            "chat": {
                "rooms": {
                    ".indexOn": ["users/user1", "users/user2"]
                }
            }
        }
    }
    

    That won't be maintainable, since you're likely adding users dynamically.

    As usual with NoSQL databases, this means you have to expand/adapt your data model to allow the use-case you want. Instead of querying the rooms for their users, keep a list of the rooms for each user (in addition to your current data):

    "user_rooms": {
      "user1": {
        "chat1": true
      },
      "user2": {
        "chat1": true,
        "chat2": true
      }
      "user3": {
        "chat2": true
      }
    

    Now you can look up the chat rooms for a user without even needing to query.

    Also see my answer about this categorization problem in Firebase query if child of child contains a value.

    0 讨论(0)
  • 2021-02-07 13:02

    I am aware that this may be a bit too late but here is something that may help you. I was looking for the same information as you, and in my case, it helped me.

    $usersId matches any child key. So you have to do something like this.

    {
    "rules": {
          "chat": {
              "rooms": {
                  "users": {
                      "$usersId": {
                        ".indexOn": ".value"
                    }
                }
            }
        }
    }
    

    My case:

    Using an unspecified index. Consider adding ".indexOn": "date" at messages/-L2bPVX4_fL41H7lhexc to your security and Firebase Database rules for better performance:

    L2bPVX4_fL41H7lhexc == child in my NoSQL database structure

    solution:

    "messages": {
      "$messagesId": {
        ".indexOn": "date"
      }
    }
    
    0 讨论(0)
提交回复
热议问题