0

I read here that it is possible to use the in keyword to query firestore, where the fields of a document are matched. I want to match the uids of the documents in my collection. Is it possible to get documents having certain uids only?

I have tried .where('id', 'in', idsArray) and .where('uid', 'in', idsArray) (only certain doc uids stored in idsArray) with no avail. Please help.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
VaibhavJoshi
  • 355
  • 2
  • 15

2 Answers2

1

If you want, based on an array of uids, to query all the docs corresponding to these uid values, you can use Promise.all() as follows:

const uids = ['abcde...', 'klmno...', 'wxyz...'];

const promises = uids.map(u => db.collection("yourcollection").doc(u).get());

Promise.all(promises).then(results => {

    //results is an array of DocumentSnapshots
    //use anny array method, like map or forEach      

    results.map(docSnapshot => {
            console.log(docSnapshot.data());
    });
});
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • So if I have 5 uids in my array, won't it make 5 different get requests? – VaibhavJoshi Dec 17 '19 at 09:16
  • Yes, but they will be done in parallel with `Promise.all()`. In terms of cost it will be the same cost than a query that returns 5 documents. If, for a specific reason, you absolutely need to use a query, you would need to add an extra field in your documents which holds the document's uid. – Renaud Tarnec Dec 17 '19 at 09:19
0

Documentation says that you can't use more than 10 identifiers.

Use the in operator to combine up to 10 equality (==) clauses on the same field with a logical OR. An in query returns documents where the given field matches any of the comparison values.

https://firebase.google.com/docs/firestore/query-data/queries#in_and_array-contains-any

Other way to access to a document by identifier is:

db.collection('users').doc(uid);
carlosvin
  • 979
  • 9
  • 22