3

Here are my Firebase database structure and my query.

enter image description here

And my query on this structure is:

var messageRef = databaseRef.child(`/messagesThread/-KdzCvVW5icW7ZuSIAxV`);

messageRef.orderByChild(`/readBy/{userId}`).equalTo(false).on("child_added", (snapshots)=>{
  console.log("Unread messages = " + snapshots.numChildren());
});

But above query giving me warning as

FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn": "readBy/{userId}".

So how to add this indexOn with dynamic {userId}?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ajay Khunti
  • 188
  • 1
  • 4

1 Answers1

3

Since indexing on a wildcard is not great, you can modify the structure and then index:

---readBy-+
          |
           294jjfouurjkfJDGLS+
                             |
                            isRead: true

Now you can add to the security rules:

"readBy": {
  ".indexOn": "isRead"
}
Idan
  • 5,405
  • 7
  • 35
  • 52
  • 1
    Yup. Also see my answer here for a longer explanation: http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Feb 28 '17 at 15:08