-
-
Save toanalien/abe560a9af40b38da0d9498fc59263c8 to your computer and use it in GitHub Desktop.
firestore query pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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