Skip to content

Commit b136ee9

Browse files
authored
Collection Group Queries (#43)
2 parents e709ef9 + c0bc11b commit b136ee9

File tree

8 files changed

+4994
-196
lines changed

8 files changed

+4994
-196
lines changed

firestore/main/.gitignore

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
firebase-debug.log*
8+
9+
# Firebase cache
10+
.firebase/
11+
12+
# Firebase config
13+
14+
# Uncomment this if you'd like others to create their own Firebase project.
15+
# For a team working on the same Firebase project(s), it is recommended to leave
16+
# it commented so all members can deploy to the same project(s) in .firebaserc.
17+
# .firebaserc
18+
19+
# Runtime data
20+
pids
21+
*.pid
22+
*.seed
23+
*.pid.lock
24+
25+
# Directory for instrumented libs generated by jscoverage/JSCover
26+
lib-cov
27+
28+
# Coverage directory used by tools like istanbul
29+
coverage
30+
31+
# nyc test coverage
32+
.nyc_output
33+
34+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
35+
.grunt
36+
37+
# Bower dependency directory (https://bower.io/)
38+
bower_components
39+
40+
# node-waf configuration
41+
.lock-wscript
42+
43+
# Compiled binary addons (http://nodejs.org/api/addons.html)
44+
build/Release
45+
46+
# Dependency directories
47+
node_modules/
48+
49+
# Optional npm cache directory
50+
.npm
51+
52+
# Optional eslint cache
53+
.eslintcache
54+
55+
# Optional REPL history
56+
.node_repl_history
57+
58+
# Output of 'npm pack'
59+
*.tgz
60+
61+
# Yarn Integrity file
62+
.yarn-integrity
63+
64+
# dotenv environment variables file
65+
.env

firestore/main/firebase.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"firestore": {
3+
"rules": "firestore.rules",
4+
"indexes": "firestore.indexes.json"
5+
}
6+
}

firestore/main/firestore.indexes.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"indexes": [
3+
{
4+
"collectionGroup": "cities",
5+
"queryScope": "COLLECTION",
6+
"fields": [
7+
{
8+
"fieldPath": "name",
9+
"order": "ASCENDING"
10+
},
11+
{
12+
"fieldPath": "state",
13+
"order": "ASCENDING"
14+
}
15+
]
16+
}
17+
],
18+
"fieldOverrides": [
19+
{
20+
"collectionGroup": "landmarks",
21+
"fieldPath": "type",
22+
"indexes": [
23+
{
24+
"order": "ASCENDING",
25+
"queryScope": "COLLECTION"
26+
},
27+
{
28+
"order": "DESCENDING",
29+
"queryScope": "COLLECTION"
30+
},
31+
{
32+
"arrayConfig": "CONTAINS",
33+
"queryScope": "COLLECTION"
34+
},
35+
{
36+
"order": "ASCENDING",
37+
"queryScope": "COLLECTION_GROUP"
38+
}
39+
]
40+
}
41+
]
42+
}

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 fs_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 fs_collection_group_query_data_setup]
870+
landmarks.then((l) => console.log(l));
871+
872+
// [START fs_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 fs_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(db);
1186+
});
11241187
});

0 commit comments

Comments
 (0)