Home > other >  Firebase, query a list of docs by a list of doc ID
Firebase, query a list of docs by a list of doc ID

Time:09-17

If we want to query one doc by its ID, we can do this:

firebase
  .firestore()
  .collection("Your collection")
  .doc("documentId")
  .get()
  .then((docRef) => { console.log(docRef.data()) })
  .catch((error) => { })

How to query a set of docs given a list of IDs, for example:

var list_of_ids = ["documentId1", "documentId2", "documentId3"]

firebase
  .firestore()
  .collection("Your collection")
  .doc(list_of_ids)
  .get()
  .then((docRef) => { console.log(docRef.data()) })
  .catch((error) => { })

Answer

Thanks Dharmaraj.

var list_of_ids = ["documentId1", "documentId2", "documentId3"]

firebase
  .firestore()
  .collection("Your collection")
  .where('__name__', 'in', list_of_ids)
  .get()
  .then((docRef) => { console.log(docRef.data()) })
  .catch((error) => { })

CodePudding user response:

If the array has less than or equal to10 IDs then you can use in operator:

.where('__name__', 'in', list_of_ids)

If the array contains more than 10 items then you would have to make separate request for each document (or make batches of 10).

  • Related