I have a collection with 30k records that I added as initial values to the db, and need to count them from time to time, since I added them only once I can't and didn't do a cloud fonction wich would increment a prop because this a root level collection.
This would be a suitable API than queriying all the docs and then count them...
db.collection('cities').count().then()
Thanks for your request. This has been requested before https://github.com/firebase/firebase-js-sdk/issues/236.
If you just need to do this occasionally it's possible to count all the documents by reading just the document keys. This is much faster than reading the entire documents. In the node.js server SDK it would look like this:
let count = 0;
db.collection('cities').select().stream()
.on('data', (snap) => {
++count;
}).on('end', () => {
console.log(`Total count is ${count}`);
});
it's possible to count all the documents by reading just the document keys. This is much faster than reading the entire documents.
@wilhuff does this method incur a read per doc, with respect to pricing? Ie. does reading just the document keys cost O(1) or O(cities) on my bill?
Trying to build an aggregation / counter using Firestore+Cloud functions which costs O(1) per update but is also idempotent. The recommended method of likes+=1 isn't idempotent, yet the cloud function docs caution that functions should be idempotent as they're executed at least once.
It's currently the case that every document returned is counted as one read, regardless of how much of the document you consume. I'm hopeful that we can eventually implement "small operations" in the way that Cloud Datastore does that would allow keys-only queries to count as a single read but we're not there yet :-(.
Thanks for quick follow-up :). Idempotent counters that cost O(1) money
would be great!
On Wed, Oct 3, 2018, 11:42 AM Gil notifications@github.com wrote:
It's currently the case that every document returned is counted as one
read, regardless of how much of the document you consume. I'm hopeful that
we can eventually implement "small operations" in the way that Cloud
Datastore does that would allow keys-only queries to count as a single read
but we're not there yet :-(.—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/firebase/firebase-js-sdk/issues/932#issuecomment-426686022,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AASRPKp5nFqMnJZ0p4C3aa9oBSINGrHzks5uhNrngaJpZM4UpjOK
.
6 months on, is there any update to this or do we rely on insane hacks to workaround a gaping hole in Firestore?
There is no update. The underlying issues described in https://github.com/firebase/firebase-js-sdk/issues/236#issuecomment-338007699 are still there.
Most helpful comment
6 months on, is there any update to this or do we rely on insane hacks to workaround a gaping hole in Firestore?