Skip to content

Commit 4280cf6

Browse files
committed
Add snippet for Collection Group Query
* Bump `@google-cloud/firestore` to `^1.3.0` which includes the new functionality
1 parent e709ef9 commit 4280cf6

File tree

3 files changed

+134
-78
lines changed

3 files changed

+134
-78
lines changed

firestore/main/index.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,65 @@ function listenErrors(db) {
820820
// [END listen_errors]
821821
}
822822

823+
function collectionGroupQuery(db) {
824+
// [START collection_group_query_data_setup]
825+
var citiesRef = db.collection('cities');
826+
827+
var landmarks = Promise.all([
828+
citiesRef.doc('SF').collection('landmarks').doc().set({
829+
name: 'Golden Gate Bridge',
830+
type: 'bridge'
831+
}),
832+
citiesRef.doc('SF').collection('landmarks').doc().set({
833+
name: 'Legion of Honor',
834+
type: 'museum'
835+
}),
836+
citiesRef.doc('LA').collection('landmarks').doc().set({
837+
name: 'Griffith Park',
838+
type: 'park'
839+
}),
840+
citiesRef.doc('LA').collection('landmarks').doc().set({
841+
name: 'The Getty',
842+
type: 'museum'
843+
}),
844+
citiesRef.doc('DC').collection('landmarks').doc().set({
845+
name: 'Lincoln Memorial',
846+
type: 'memorial'
847+
}),
848+
citiesRef.doc('DC').collection('landmarks').doc().set({
849+
name: 'National Air and Space Museum',
850+
type: 'museum'
851+
}),
852+
citiesRef.doc('TOK').collection('landmarks').doc().set({
853+
name: 'Ueno Park',
854+
type: 'park'
855+
}),
856+
citiesRef.doc('TOK').collection('landmarks').doc().set({
857+
name: 'National Museum of Nature and Science',
858+
type: 'museum'
859+
}),
860+
citiesRef.doc('BJ').collection('landmarks').doc().set({
861+
name: 'Jingshan Park',
862+
type: 'park'
863+
}),
864+
citiesRef.doc('BJ').collection('landmarks').doc().set({
865+
name: 'Beijing Ancient Observatory',
866+
type: 'museum'
867+
})
868+
]);
869+
// [END collection_group_query_data_setup]
870+
landmarks.then((l) => console.log(l));
871+
872+
// [START collection_group_query]
873+
let museums = db.collectionGroup('landmarks').where('type', '==', 'museum');
874+
museums.get().then(function(querySnapshot) {
875+
querySnapshot.forEach(function(doc) {
876+
console.log(doc.id, ' => ', doc.data());
877+
});
878+
});
879+
// [end collection_group_query]
880+
}
881+
823882
// ============================================================================
824883
// https://firebase.google.com/docs/firestore/query-data/query-cursors
825884
// ============================================================================
@@ -1121,4 +1180,8 @@ describe('Firestore Smoketests', () => {
11211180
it('should delete the whole collection', () => {
11221181
return deleteCollection(db, 'cities', 50);
11231182
});
1183+
1184+
it('should find all museums when querying a collection group', () => {
1185+
return collectionGroupQuery();
1186+
});
11241187
});

0 commit comments

Comments
 (0)