Search code examples
node.jsgoogle-cloud-firestoregoogle-cloud-functionsfirebase-admin

How to use count() method in Firestore transaction?


I have a callable cloud function (node.js) which runs a transaction with numerous writes and reads. One of those reads should be a count() read:

const count = await transaction.get(db.collection("posts").count());

However, this doesn't seem to work as I get an error. Perhaps this method isn't yet a part of transactions?


Solution

  • The support for count() queries was added in firebase-admin@11.2.0 and transaction.get() takes either a DocumentReference or a Query (also AggregateQuery) so that should work. Try updating to latest version of firebase-admin.

    npm i firebase-admin@latest