Skip to content

Add count query snippet #307

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 5 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 21 additions & 0 deletions firestore-next/test.firestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,27 @@ describe("firestore", () => {
});
});

describe("aggregate queries", () => {
it("should fetch the count of documents in a collection", async () => {
const { collection, getCountFromServer } = require("firebase/firestore");
// [START count_aggregate_collection]
const coll = collection(db, "cities");
const snapshot = await getCountFromServer(coll);
console.log('count: ', snapshot.data().count);
// [END count_aggregate_collection]
});

it("should fetch the count of documents in a query", async () => {
const { collection, getCountFromServer, where, query } = require("firebase/firestore");
// [START count_aggregate_query]
const coll = collection(db, "cities");
const q = query(coll, where("state", "==", "CA"));
const snapshot = await getCountFromServer(q);
console.log('count: ', snapshot.data().count);
// [END count_aggregate_query]
});
});

// TODO: Break out into separate file
describe("solution-aggregation", () => {
it("should update a restaurant in a transaction #UNVERIFIED", async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This snippet file was generated by processing the source file:
// ./firestore-next/test.firestore.js
//
// To update the snippets in this file, edit the source and then run
// 'npm run snippets'.

// [START count_aggregate_collection_modular]
const coll = collection(db, "cities");
const snapshot = await getCountFromServer(coll);
console.log('count: ', snapshot.data().count);
// [END count_aggregate_collection_modular]
12 changes: 12 additions & 0 deletions snippets/firestore-next/test-firestore/count_aggregate_query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This snippet file was generated by processing the source file:
// ./firestore-next/test.firestore.js
//
// To update the snippets in this file, edit the source and then run
// 'npm run snippets'.

// [START count_aggregate_query_modular]
const coll = collection(db, "cities");
const q = query(coll, where("state", "==", "CA"));
const snapshot = await getCountFromServer(q);
console.log('count: ', snapshot.data().count);
// [END count_aggregate_query_modular]