0
votes

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.

2
Yes, I work for me, and Thanks to help meAzhar Ali

2 Answers

2
votes

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') { ... }
  }
});
0
votes

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.