0

Is there any way to search with array in realtime database as cloud database provides us. Cloud example:

.collection("bids")
.where("title", "array-contains-any", ["xx", "yy", "zz"])

In above query I got all bids where title is xx, yy, zz. How do we can search like this in firebase realtime database.

Frank van Puffelen
  • 499,950
  • 69
  • 739
  • 734
Azhar Ali
  • 1,259
  • 1
  • 7
  • 17

2 Answers2

3

Actually in real-time database there is no any thing like "array-contains". There for if you have an array you have to deal with just like a regular JavaScript array.

In your case you can try this.

var ref = firebase.database().ref('/bids/title');
ref.once('value').then(function(snap) {
  var array = snap.val();
  for (var i in array) {
    var value = array[i]
    console.log(value);
    if (value == 'xx') { ... }
  }
});
Nimna Perera
  • 797
  • 8
  • 15
0

There is no equivalent operation in the Realtime Database.

The common approach for this type of use-case, is to set up an additional data structure mapping from bid titles to bids as shown here: Firebase query if child of child contains a value

You can then read the bid IDs for each title with a separate query, look up the additional properties for each bid with another call, and then merge all results in your client-side application code. This is not nearly as slow as you may initially expect, as Firebase pipelines the requests over a single websocket connection.

Frank van Puffelen
  • 499,950
  • 69
  • 739
  • 734