Skip to content

Commit 36812ac

Browse files
authored
added count aggregation query snippets (#19)
1 parent 2c49691 commit 36812ac

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

packages/firebase_snippets_app/lib/snippets/firestore.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,33 @@ class FirestoreSnippets extends DocSnippet {
804804
// [END perform_simple_and_compound_queries_collection_groups2]
805805
}
806806

807+
void aggregationQuery_count() {
808+
// [START count_aggregate_collection]
809+
// Returns number of documents in users collection
810+
db
811+
.collection("users")
812+
.count()
813+
.then(
814+
(res) => print(res.data().count),
815+
onError: (e) => print("Error completing: $e"),
816+
);
817+
// [END count_aggregate_collection]
818+
}
819+
820+
void aggregationQuery_count2() {
821+
// [START count_aggregate_query]
822+
// This also works with collectionGroup queries.
823+
db
824+
.collection("users")
825+
.where("age", isGreaterThan: 10)
826+
.count()
827+
.then(
828+
(res) => print(res.data().count),
829+
onError: (e) => print("Error completing: $e"),
830+
);
831+
// [END count_aggregate_query]
832+
}
833+
807834
void orderAndLimitData_orderAndLimitData() {
808835
// [START order_and_limit_data_order_and_limit_data]
809836
final citiesRef = db.collection("cities");

0 commit comments

Comments
 (0)