Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 0b9b250

Browse files
Add support for Firestore Collection Group queries #1284
1 parent c47106c commit 0b9b250

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

demo-ng/app/tabs/firestore/firestore.component.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,12 @@ export class FirestoreComponent {
398398
firestoreCollectionGroupQuery(): void {
399399
firebase.firestore().collectionGroup("cities").where("population", ">=", 1_000_000)
400400
.get()
401-
.then(result => console.log(JSON.stringify(result)))
402-
.catch(err => console.log("Delete failed, error: " + err));
401+
.then(querySnapshot => {
402+
querySnapshot.forEach(doc => {
403+
console.log(`City with >= 1M population: ${doc.id} => ${JSON.stringify(doc.data())}`);
404+
});
405+
})
406+
.catch(err => console.log("Querying collection group failed, error: " + err));
403407
}
404408

405409
doWebGetValueForCompanies(): void {

docs/FIRESTORE.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,23 @@ query
255255
});
256256
```
257257

258+
### [Collection Group Queries](https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query): `collectionGroup.where()`
259+
A collection group consists of all collections with the same ID. By default, queries retrieve results from a single collection in your database. Use a collection group query to retrieve documents from a collection group instead of from a single collection.
260+
261+
Note that this will require an *index* in your db, so make sure to `catch` any errors when invoking this method
262+
and log out any error messages so you can easily copy-paste the required index into your browser URL bar.
263+
264+
```typescript
265+
// "Gimme all cities with a population of at least a million"
266+
firebase.firestore().collectionGroup("cities").where("population", ">=", 1_000_000)
267+
.then(querySnapshot => {
268+
querySnapshot.forEach(doc => {
269+
console.log(`City with >= 1M population: ${doc.id} => ${JSON.stringify(doc.data())}`);
270+
});
271+
})
272+
.catch(err => console.log("Querying collection group failed, error: " + err));
273+
```
274+
258275
### Delete an entire document: `collection.doc().delete()`
259276
Entirely remove a document from a collection:
260277

docs/STORAGE.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,6 @@ In this example we'll determine the remote URL of the previously uploaded file.
212212
Note that your security rules must be version "2" for this to work,
213213
so if it fails try adding this at the top of your rules defined for your storage bucket: `rules_version = '2';`
214214

215-
Also note that this will require an index in your db, so make sure to `catch` any errors when invoking this method
216-
and log out any error messages so you can easily copy-paste the required index into your browser URL bar.
217-
218215
<details>
219216
<summary>Native API</summary>
220217

@@ -230,7 +227,6 @@ and log out any error messages so you can easily copy-paste the required index i
230227
// see the Web API example below for an advanced example
231228
},
232229
function (error) {
233-
// probably related to a missing index or security rules, see the notes in the readme above
234230
console.log(error);
235231
}
236232
);

0 commit comments

Comments
 (0)