Skip to content

Commit 4738eab

Browse files
authored
Add count query snippets (#303)
* Add count query snippets * execute snippet * upaate non-breaking changes * update node modules to latest
1 parent f1869ee commit 4738eab

File tree

3 files changed

+5981
-5535
lines changed

3 files changed

+5981
-5535
lines changed

firestore/main/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,25 @@ async function deleteQueryBatch(db, query, resolve) {
982982

983983
// [END firestore_data_delete_collection]
984984

985+
async function countAggregateCollection(db) {
986+
// [START count_aggregation_collection]
987+
const collectionRef = db.collection('cities');
988+
const snapshot = await collectionRef.count().get();
989+
990+
console.log(snapshot.data.count);
991+
// [END count_aggregation_collection]
992+
}
993+
994+
async function countAggregateQuery(db) {
995+
// [START count_aggregation_query]
996+
const collectionRef = db.collection('cities');
997+
const query = collectionRef.where('state', '==', 'CA');
998+
const snapshot = await collectionRef.count().get();
999+
1000+
console.log(snapshot.data.count);
1001+
// [END count_aggregation_query]
1002+
}
1003+
9851004
// ============================================================================
9861005
// MAIN
9871006
// ============================================================================
@@ -1165,4 +1184,12 @@ describe('Firestore Smoketests', () => {
11651184
it('should find all museums when querying a collection group', () => {
11661185
return collectionGroupQuery(db);
11671186
});
1187+
1188+
it('should count the number of documents in a collection', () => {
1189+
return countAggregateCollection(db);
1190+
});
1191+
1192+
it('should count the number of documents in a filtered query', () => {
1193+
return countAggregateQuery(db);
1194+
});
11681195
});

0 commit comments

Comments
 (0)