Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add snippets for OR queries #364

Merged
merged 11 commits into from
Jun 1, 2023
36 changes: 36 additions & 0 deletions firestore/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,38 @@ async function inQueries(db) {
console.log('Exactly One Coast get: ', exactlyOneCoast);
}

/**
* Demonstrate OR queries
*
* @param {FirebaseFirestore.Firestore} db
*/
async function orQueries(db) {
const citiesRef = db.collection("cities");

// [START firestore_query_or]
const bigCities = await citiesRef.where(
citiesRef.or(
citiesRef.where("capital", "==", true),
citiesRef.where("population", ">=", 1000000)
)
).get();
// [END firestore_query_or]

// [START firestore_query_or_compound]
const bigCitiesInCalifornia = await citiesRef
.where("state", "==", "CA")
.where(
citiesRef.or(
citiesRef.where("capital", "==", true),
citiesRef.where("population", ">=", 1000000)
)
).get();
// [END firestore_query_or_compound]

console.log('Big cities get: ', bigCities);
console.log('Big cities in California get: ', bigCitiesInCalifornia);
}

async function orderAndLimit(db) {
const citiesRef = db.collection('cities');
// [START firestore_query_order_limit]
Expand Down Expand Up @@ -1071,6 +1103,10 @@ describe('Firestore Smoketests', () => {
return inQueries(db);
});

it('should support or queries', () => {
return orQueries(db);
});

it('should order and limit', () => {
return orderAndLimit(db);
});
Expand Down
Loading