Is there a workaround for the Firebase Query "IN" Limit to 10?
23 Jan 2024, 12:17 UTC
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
let query = config.db
.collection(USER_COLLECTION_NAME)
.where("id", "in", matchesIdArray);
const users = await query.get();
(matchesIdArray.length needs to be unlimited)
1 answer
Accepted answer · original discussion
27 Jan 2024, 02:41 UTC
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());
}
3 question comments
Use comments to ask for clarification. Post a solution as an answer.
24 Jan 2024, 17:05 UTC
25 Jan 2024, 21:53 UTC
28 Jan 2024, 07:29 UTC
not-in operator, the limit of 10 comparison values was increased to 30 (across your query) with the release of OR queries in March 2023. The change was made available in the following JavaScript SDK versions: firebase v9.18.0, @google-cloud/firestore v6.5.0, and firebase-admin v11.6.0.