Skip to content

Commit 2b96a4c

Browse files
Lyokonethatfiredev
andauthored
feat(firestore): add samples for sum and average (#37)
--------- Co-authored-by: rosariopf <rosariopf@google.com>
1 parent 36a9c52 commit 2b96a4c

File tree

1 file changed

+87
-18
lines changed

1 file changed

+87
-18
lines changed

packages/firebase_snippets_app/lib/snippets/firestore.dart

Lines changed: 87 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -842,32 +842,33 @@ class FirestoreSnippets extends DocSnippet {
842842

843843
void filterQuery_or() {
844844
// [START firestore_query_filter_or]
845-
var query = db.collection("cities")
846-
.where(
847-
Filter.or(
848-
Filter("capital", isEqualTo: true),
849-
Filter("population", isGreaterThan: 1000000)
850-
));
845+
var query = db.collection("cities").where(
846+
Filter.or(
847+
Filter("capital", isEqualTo: true),
848+
Filter("population", isGreaterThan: 1000000),
849+
),
850+
);
851851
// [END firestore_query_filter_or]
852852
}
853853

854854
void filterQuery_or2() {
855855
// [START firestore_query_filter_or_compound]
856-
var query = db.collection("cities")
857-
.where(
858-
Filter.and(
859-
Filter("state", isEqualTo: "CA"),
860-
Filter.or(
861-
Filter("capital", isEqualTo: true),
862-
Filter("population", isGreaterThan: 1000000)
863-
)));
856+
var query = db.collection("cities").where(
857+
Filter.and(
858+
Filter("state", isEqualTo: "CA"),
859+
Filter.or(
860+
Filter("capital", isEqualTo: true),
861+
Filter("population", isGreaterThan: 1000000),
862+
),
863+
),
864+
);
864865
// [END firestore_query_filter_or_compound]
865866
}
866867

867868
void aggregationQuery_count() {
868869
// [START count_aggregate_collection]
869870
// Returns number of documents in users collection
870-
db.collection("users").count().get().then(
871+
db.collection("cities").count().get().then(
871872
(res) => print(res.count),
872873
onError: (e) => print("Error completing: $e"),
873874
);
@@ -876,14 +877,81 @@ class FirestoreSnippets extends DocSnippet {
876877

877878
void aggregationQuery_count2() {
878879
// [START count_aggregate_query]
879-
// This also works with collectionGroup queries.
880-
db.collection("users").where("age", isGreaterThan: 10).count().get().then(
880+
// This also works with collection queries.
881+
db.collection("cities").where("capital", isEqualTo: 10).count().get().then(
881882
(res) => print(res.count),
882883
onError: (e) => print("Error completing: $e"),
883884
);
884885
// [END count_aggregate_query]
885886
}
886887

888+
void aggregationQuery_sum() {
889+
// [START sum_aggregate_collection]
890+
db.collection("cities").aggregate(sum("population")).get().then(
891+
(res) => print(res.getAverage("population")),
892+
onError: (e) => print("Error completing: $e"),
893+
);
894+
// [END sum_aggregate_collection]
895+
}
896+
897+
void aggregationQuery_sum2() {
898+
// [START sum_aggregate_query]
899+
db
900+
.collection("cities")
901+
.where("capital", isEqualTo: true)
902+
.aggregate(sum("population"))
903+
.get()
904+
.then(
905+
(res) => print(res.getAverage("population")),
906+
onError: (e) => print("Error completing: $e"),
907+
);
908+
// [END sum_aggregate_query]
909+
}
910+
911+
void aggregationQuery_average() {
912+
// [START average_aggregate_collection]
913+
db.collection("cities").aggregate(average("population")).get().then(
914+
(res) => print(res.getAverage("population")),
915+
onError: (e) => print("Error completing: $e"),
916+
);
917+
// [END average_aggregate_collection]
918+
}
919+
920+
void aggregationQuery_average2() {
921+
// [START average_aggregate_query]
922+
db
923+
.collection("cities")
924+
.where("capital", isEqualTo: true)
925+
.aggregate(average("population"))
926+
.get()
927+
.then(
928+
(res) => print(res.getAverage("population")),
929+
onError: (e) => print("Error completing: $e"),
930+
);
931+
// [END average_aggregate_query]
932+
}
933+
934+
void multipleAggregateQueries() {
935+
// [START multiple_aggregate_queries]
936+
db
937+
.collection("cities")
938+
.aggregate(
939+
count(),
940+
sum("population"),
941+
average("population"),
942+
)
943+
.get()
944+
.then(
945+
(res) {
946+
print(res.count);
947+
print(res.getSum("population"));
948+
print(res.getAverage("population"));
949+
},
950+
onError: (e) => print("Error completing: $e"),
951+
);
952+
// [END multiple_aggregate_queries]
953+
}
954+
887955
void orderAndLimitData_orderAndLimitData() {
888956
// [START order_and_limit_data_order_and_limit_data]
889957
final citiesRef = db.collection("cities");
@@ -970,7 +1038,8 @@ class FirestoreSnippets extends DocSnippet {
9701038
final next = db
9711039
.collection("cities")
9721040
.orderBy("population")
973-
.startAfterDocument(lastVisible).limit(25);
1041+
.startAfterDocument(lastVisible)
1042+
.limit(25);
9741043

9751044
// Use the query for pagination
9761045
// ...

0 commit comments

Comments
 (0)