Skip to content

Commit 36740fb

Browse files
authored
Add count query snippet (#307)
* Add count query snippet * add simpler snippet * consts * no masking * run snippets
1 parent 476223e commit 36740fb

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

firestore-next/test.firestore.js

+21
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,27 @@ describe("firestore", () => {
11581158
});
11591159
});
11601160

1161+
describe("aggregate queries", () => {
1162+
it("should fetch the count of documents in a collection", async () => {
1163+
const { collection, getCountFromServer } = require("firebase/firestore");
1164+
// [START count_aggregate_collection]
1165+
const coll = collection(db, "cities");
1166+
const snapshot = await getCountFromServer(coll);
1167+
console.log('count: ', snapshot.data().count);
1168+
// [END count_aggregate_collection]
1169+
});
1170+
1171+
it("should fetch the count of documents in a query", async () => {
1172+
const { collection, getCountFromServer, where, query } = require("firebase/firestore");
1173+
// [START count_aggregate_query]
1174+
const coll = collection(db, "cities");
1175+
const q = query(coll, where("state", "==", "CA"));
1176+
const snapshot = await getCountFromServer(q);
1177+
console.log('count: ', snapshot.data().count);
1178+
// [END count_aggregate_query]
1179+
});
1180+
});
1181+
11611182
// TODO: Break out into separate file
11621183
describe("solution-aggregation", () => {
11631184
it("should update a restaurant in a transaction #UNVERIFIED", async () => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./firestore-next/test.firestore.js
3+
//
4+
// To update the snippets in this file, edit the source and then run
5+
// 'npm run snippets'.
6+
7+
// [START count_aggregate_collection_modular]
8+
const coll = collection(db, "cities");
9+
const snapshot = await getCountFromServer(coll);
10+
console.log('count: ', snapshot.data().count);
11+
// [END count_aggregate_collection_modular]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./firestore-next/test.firestore.js
3+
//
4+
// To update the snippets in this file, edit the source and then run
5+
// 'npm run snippets'.
6+
7+
// [START count_aggregate_query_modular]
8+
const coll = collection(db, "cities");
9+
const q = query(coll, where("state", "==", "CA"));
10+
const snapshot = await getCountFromServer(q);
11+
console.log('count: ', snapshot.data().count);
12+
// [END count_aggregate_query_modular]

0 commit comments

Comments
 (0)