Skip to content

Instantly share code, notes, and snippets.

@toanalien
Created June 3, 2022 16:57
Show Gist options
  • Save toanalien/abe560a9af40b38da0d9498fc59263c8 to your computer and use it in GitHub Desktop.
Save toanalien/abe560a9af40b38da0d9498fc59263c8 to your computer and use it in GitHub Desktop.
firestore query pagination
let sum = 0;
let last = null
let snapshot = null
while (true) {
if (last) {
snapshot = await db.collection('count').where('amount', '>', 0).select('amount').limit(1000).startAfter(last).get()
} else {
snapshot = await db.collection('count').where('amount', '>', 0).select('amount').limit(1000).get()
}
snapshot.forEach(doc => {
sum += doc.data()['amount']
})
let len = snapshot.docs.length;
if (len < 1000) {
break
} else {
last = snapshot.docs[snapshot.docs.length - 1];
}
}
console.log({sum})
let sum = 0;
snapshot = await db.collection('count').where('amount', '>', 0).select('amount').get()
snapshot.forEach(doc => {
sum += doc.data()['amount']
})
console.log({sum})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment