@@ -2,7 +2,7 @@ const debug = require('debug')('firestore-snippets-node');
22
33// [START firestore_deps]
44const { initializeApp, applicationDefault, cert } = require ( 'firebase-admin/app' ) ;
5- const { getFirestore, Timestamp, FieldValue } = require ( 'firebase-admin/firestore' ) ;
5+ const { getFirestore, Timestamp, FieldValue, Filter } = require ( 'firebase-admin/firestore' ) ;
66// [END firestore_deps]
77
88
@@ -647,6 +647,41 @@ async function inQueries(db) {
647647 console . log ( 'Exactly One Coast get: ' , exactlyOneCoast ) ;
648648}
649649
650+ /**
651+ * Demonstrate OR queries
652+ *
653+ * @param {FirebaseFirestore.Firestore } db
654+ */
655+ async function orQueries ( db ) {
656+ const citiesRef = db . collection ( 'cities' ) ;
657+
658+ // [START firestore_query_or]
659+ const bigCities = await citiesRef
660+ . where (
661+ Filter . or (
662+ Filter . where ( 'capital' , '==' , true ) ,
663+ Filter . where ( 'population' , '>=' , 1000000 )
664+ )
665+ )
666+ . get ( ) ;
667+ // [END firestore_query_or]
668+
669+ // [START firestore_query_or_compound]
670+ const bigCitiesInCalifornia = await citiesRef
671+ . where ( 'state' , '==' , 'CA' )
672+ . where (
673+ Filter . or (
674+ Filter . where ( 'capital' , '==' , true ) ,
675+ Filter . where ( 'population' , '>=' , 1000000 )
676+ )
677+ )
678+ . get ( ) ;
679+ // [END firestore_query_or_compound]
680+
681+ console . log ( 'Big cities get: ' , bigCities ) ;
682+ console . log ( 'Big cities in California get: ' , bigCitiesInCalifornia ) ;
683+ }
684+
650685async function orderAndLimit ( db ) {
651686 const citiesRef = db . collection ( 'cities' ) ;
652687 // [START firestore_query_order_limit]
@@ -1071,6 +1106,10 @@ describe('Firestore Smoketests', () => {
10711106 return inQueries ( db ) ;
10721107 } ) ;
10731108
1109+ it ( 'should support or queries' , ( ) => {
1110+ return orQueries ( db ) ;
1111+ } ) ;
1112+
10741113 it ( 'should order and limit' , ( ) => {
10751114 return orderAndLimit ( db ) ;
10761115 } ) ;
0 commit comments