0

for example, I have an Array like this ["10154924352473317", "10155028001267749", '11220393911113', '11220393911112', '11220393911111']

in firebase BD I have a collection of Users which has the ID field like above, and I want to fetch all users with ID which in my Array, I don't want to make a request for each User, it should be one request, it is possible?

https://image.prntscr.com/image/K2vFENsFTBOBRujYHwVF_A.png

Andrey Shostik
  • 164
  • 2
  • 13
  • It appears that the query you want is not supported currently, https://stackoverflow.com/questions/29560088/firebase-equivalent-to-sql-where-in/29564075#29564075 – agit Nov 02 '17 at 13:03

1 Answers1

2

If the ids are monotonically increasing integers get the lowest id and the highest id, then try this:

var lowestId = ...;
var highestId = ...;

ref.orderByKey().startAt(lowestId).endAt(highestId)
  .on("child_added", function(snapshot){
      ...
  });

Method 2

It is recommended that clients make only one request, not multiple, so you can spin up a cloud function which makes multiple requests to the database (or takes all users, depending on how many ids you are querying for), filters them by ids and returns the result.

Alexander Vitanov
  • 4,074
  • 2
  • 19
  • 22
  • Can select fields which to fetch? I do not want to fetch all, only those which I will select http://prntscr.com/h5a7cj – Andrey Shostik Nov 02 '17 at 13:46
  • With querying - only if you add another node, like `usersMetaInfo` where you store only those fields. With method 2 - yes, you choose what to return – Alexander Vitanov Nov 02 '17 at 13:49
  • The Firebase Realtime Database always returns full nodes. You cannot select a specific property from each matching child node. See https://stackoverflow.com/questions/39596678/nosql-data-structure-to-select-subset-of-fields-with-firebase-and-query – Frank van Puffelen Nov 02 '17 at 14:54