I have run into the firebase “IN” limit of 10. Although a workaround solution was already answered here:
Is there a workaround for the Firebase Query "IN" Limit to 10?
None of the solutions in that thread seem to work with the listener “onSnapshot”. For my use case (Vue 3), I have a composable/function call I that queries firebase passing in an array that could have up to 100 document ID values and returns an object as below.
Is this possible?
import { ref, watchEffect } from 'vue'
import { db } from '@/firebase/config'
import { collection, onSnapshot, query, where, documentId } from 'firebase/firestore'
const getUsersList = (idList) => {
// idList is an array of document ID's
const documents = ref(null)
let collectionRef = collection(db, 'users')
collectionRef = query(collectionRef, where(documentId(), 'in', idList))
// this fails if I pass in more than 10 elements in the array
const unsub = onSnapshot(collectionRef, snapshot => {
let results = []
snapshot.docs.forEach(doc => {
results.push({ ...doc.data(), id: doc.id })
})
// update values
documents.value = results
})
watchEffect((onInvalidate) => {
onInvalidate(() => unsub())
})
return { documents }
}
export default getCollectionRt
2
Answers
Since no replies here completely answered the question, I ended up paying a freelancer to take a look and here's what they came up with. The solution does seem to have a random issue I am trying to sort out when the underlying changes, one of the records will disappear. It does work, is in scope of the original question and seems to have solved the problem.
You will have to initialize multiple listeners i.e. same number of queries but with
onSnapshot()
(might be better than setting up a listener for each individual document). Try:The
dataLoaded
flag checks if the listeners have fetched data for first time or has received an update. I’ve use a map where the key is document ID so it can be removed easily but do change that to an array or any other required structure.