Skip to content

feat(firestore): add samples for sum and average #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/firebase_snippets_app/lib/snippets/firestore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,52 @@ class FirestoreSnippets extends DocSnippet {
// [END count_aggregate_query]
}

void aggregationQuery_sum() {
// [START sum_aggregate_collection]
db.collection("users").aggregate(sum("age")).get().then(
(res) => print(res.getAverage("age")),
onError: (e) => print("Error completing: $e"),
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
db.collection("users").aggregate(sum("age")).get().then(
(res) => print(res.getAverage("age")),
onError: (e) => print("Error completing: $e"),
);
db.collection("cities").aggregate(sum("population")).get().then(
(res) => print(res.getAverage("population")),
onError: (e) => print("Error completing: $e"),
);

// [END sum_aggregate_collection]
}

void aggregationQuery_sum2() {
// [START sum_aggregate_query]
db
.collection("users")
.where("age", isGreaterThan: 10)
.aggregate(sum("age"))
.get()
.then(
(res) => print(res.getAverage("age")),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.collection("users")
.where("age", isGreaterThan: 10)
.aggregate(sum("age"))
.get()
.then(
(res) => print(res.getAverage("age")),
.collection("cities")
.where("capital", isEqualTo: true)
.aggregate(sum("population"))
.get()
.then(
(res) => print(res.getAverage("population")),

onError: (e) => print("Error completing: $e"),
);
// [END sum_aggregate_query]
}

void aggregationQuery_average() {
// [START average_aggregate_collection]
db.collection("users").aggregate(average("age")).get().then(
(res) => print(res.getAverage("age")),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
db.collection("users").aggregate(average("age")).get().then(
(res) => print(res.getAverage("age")),
db.collection("cities").aggregate(average("population")).get().then(
(res) => print(res.getAverage("population")),

onError: (e) => print("Error completing: $e"),
);
// [END average_aggregate_collection]
}

void aggregationQuery_average2() {
// [START average_aggregate_query]
db
.collection("users")
.where("age", isGreaterThan: 10)
.aggregate(average("age"))
.get()
.then(
(res) => print(res.getAverage("age")),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.collection("users")
.where("age", isGreaterThan: 10)
.aggregate(average("age"))
.get()
.then(
(res) => print(res.getAverage("age")),
.collection("cities")
.where("capital", isEqualTo: true)
.aggregate(average("population"))
.get()
.then(
(res) => print(res.getAverage("population")),

onError: (e) => print("Error completing: $e"),
);
// [END average_aggregate_query]
}

void orderAndLimitData_orderAndLimitData() {
// [START order_and_limit_data_order_and_limit_data]
final citiesRef = db.collection("cities");
Expand Down
Loading