Technical questionAccepted answer
Is there a workaround for the Firebase Query "IN" Limit to 10?
Olivia
Apr 21
0 views1
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
Conrad Davis jr.
PermalinkFeb 18
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.
samthecodingmanPermalink
Dec 1
For future readers, with the exception of the
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.