Search code examples
javascriptfirebasereact-nativegoogle-cloud-platformgoogle-cloud-firestore

Is there a workaround for the Firebase Query "IN" Limit to 10?


I have a query for firebase that has an array of IDs that has a size > 10. Firebase has restrictions on the number of records to query in one session. Is there a way to query against more than 10 at a time?

[Unhandled promise rejection: FirebaseError: Invalid Query. 'in' filters support a maximum of 10 elements in the value array.]

https://cloud.google.com/firestore/docs/query-data/queries

enter image description here

  let query = config.db
    .collection(USER_COLLECTION_NAME)
    .where("id", "in", matchesIdArray);
  const users = await query.get();

(matchesIdArray.length needs to be unlimited)


Solution

  • I found this to work well for me without needing to make as many queries (loop and request in batches of 10).

    export async function getContentById(ids, path) {
      // don't run if there aren't any ids or a path for the collection
      if (!ids || !ids.length || !path) return [];
    
      const collectionPath = db.collection(path);
      const batches = [];
    
      while (ids.length) {
        // firestore limits batches to 10
        const batch = ids.splice(0, 10);
    
        // add the batch request to to a queue
        batches.push(
          collectionPath
            .where(
              firebase.firestore.FieldPath.documentId(),
              'in',
              [...batch]
            )
            .get()
            .then(results => results.docs.map(result => ({ /* id: result.id, */ ...result.data() }) ))
        )
      }
    
      // after all of the data is fetched, return it
      return Promise.all(batches)
        .then(content => content.flat());
    }