From 62b1229258a52660328319a256dc97b3ac15cded Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Thu, 15 Sep 2022 15:08:04 -0700 Subject: [PATCH 1/5] Add count query snippet --- firestore-next/test.firestore.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/firestore-next/test.firestore.js b/firestore-next/test.firestore.js index 17ff6d29..a24e7676 100644 --- a/firestore-next/test.firestore.js +++ b/firestore-next/test.firestore.js @@ -1158,6 +1158,17 @@ describe("firestore", () => { }); }); + describe("aggregate queries", () => { + it("should fetch the count of documents in a collection", async () => { + // [START count_aggregate_query] + const coll = collection(db, "games/chess/players"); + const query = query(coll, where("online", "==", true)); + const snapshot = await getCountFromServer(query); + 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 () => { From 09d36f3b62c73f44e57f5aa1d3ccdd39acc00014 Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Thu, 29 Sep 2022 11:03:42 -0700 Subject: [PATCH 2/5] add simpler snippet --- firestore-next/test.firestore.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/firestore-next/test.firestore.js b/firestore-next/test.firestore.js index a24e7676..2abc2f06 100644 --- a/firestore-next/test.firestore.js +++ b/firestore-next/test.firestore.js @@ -1160,9 +1160,17 @@ describe("firestore", () => { describe("aggregate queries", () => { it("should fetch the count of documents in a collection", async () => { + // [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 () => { // [START count_aggregate_query] - const coll = collection(db, "games/chess/players"); - const query = query(coll, where("online", "==", true)); + const coll = collection(db, "cities"); + const query = query(coll, where("state", "==", "CA")); const snapshot = await getCountFromServer(query); console.log('count: ', snapshot.data().count); // [END count_aggregate_query] From 49e0e36469a5a2ececf20e0bb9355e6d079359de Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Thu, 20 Oct 2022 15:03:37 -0700 Subject: [PATCH 3/5] consts --- firestore-next/test.firestore.js | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/firestore-next/test.firestore.js b/firestore-next/test.firestore.js index 2abc2f06..375e9a80 100644 --- a/firestore-next/test.firestore.js +++ b/firestore-next/test.firestore.js @@ -1159,22 +1159,24 @@ describe("firestore", () => { }); describe("aggregate queries", () => { - it("should fetch the count of documents in a collection", async () => { - // [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 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 () => { - // [START count_aggregate_query] - const coll = collection(db, "cities"); - const query = query(coll, where("state", "==", "CA")); - const snapshot = await getCountFromServer(query); - console.log('count: ', snapshot.data().count); - // [END count_aggregate_query] - }); + 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 query = query(coll, where("state", "==", "CA")); + const snapshot = await getCountFromServer(query); + console.log('count: ', snapshot.data().count); + // [END count_aggregate_query] + }); }); // TODO: Break out into separate file From 9b09fe0059f16a77e3065148a5ace3cbd21a1153 Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Thu, 20 Oct 2022 15:07:20 -0700 Subject: [PATCH 4/5] no masking --- firestore-next/test.firestore.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firestore-next/test.firestore.js b/firestore-next/test.firestore.js index 375e9a80..2469b736 100644 --- a/firestore-next/test.firestore.js +++ b/firestore-next/test.firestore.js @@ -1172,8 +1172,8 @@ describe("firestore", () => { const { collection, getCountFromServer, where, query } = require("firebase/firestore"); // [START count_aggregate_query] const coll = collection(db, "cities"); - const query = query(coll, where("state", "==", "CA")); - const snapshot = await getCountFromServer(query); + const q = query(coll, where("state", "==", "CA")); + const snapshot = await getCountFromServer(q); console.log('count: ', snapshot.data().count); // [END count_aggregate_query] }); From ff2ca7e77be324f19458507867bf9b5ff7de16e8 Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Thu, 20 Oct 2022 15:10:52 -0700 Subject: [PATCH 5/5] run snippets --- .../test-firestore/count_aggregate_collection.js | 11 +++++++++++ .../test-firestore/count_aggregate_query.js | 12 ++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 snippets/firestore-next/test-firestore/count_aggregate_collection.js create mode 100644 snippets/firestore-next/test-firestore/count_aggregate_query.js diff --git a/snippets/firestore-next/test-firestore/count_aggregate_collection.js b/snippets/firestore-next/test-firestore/count_aggregate_collection.js new file mode 100644 index 00000000..7dca7e37 --- /dev/null +++ b/snippets/firestore-next/test-firestore/count_aggregate_collection.js @@ -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] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/count_aggregate_query.js b/snippets/firestore-next/test-firestore/count_aggregate_query.js new file mode 100644 index 00000000..37a27458 --- /dev/null +++ b/snippets/firestore-next/test-firestore/count_aggregate_query.js @@ -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] \ No newline at end of file