1
votes

I'm building a chat with firebase realtime database.

Let's say user1 has uid1 and user2 has uid2, their chatId will be uid1_uid2 (ordered asc). So the database chat/ looks like this:

chat {
  uid1_uid2: [message, message...],
  uid4_uid2:[message, message...],
  uid5_uid1:[message, message...],
  uid11_uid8:[message, message...]
}

Firebase rules are the following to allow only the interlocutors to have access to the conversation:

   "chat": {
          "$chatId": {
            ".write": "$chatId.contains(auth.uid)",
            ".read": "$chatId.contains(auth.uid)"
          }
          }

I want to get elements having uid1 in the chatId. So the expected return would be

{
  uid1_uid2: [message, message...],
  uid5_uid1:[message, message...]
}

Is there a way to get elements whose id contains a particular substring ? Something like...

databaseChat.child("contains uid1").on("value", snapshot => {

})
1

1 Answers

1
votes

This is not possible. Realtime Database doesn't have substring queries. The best you can do is find prefix strings in values.

How to query firebase keys with a substring