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

Commit 9942fe5

Browse files
Support Firestore "in" queries #1472
1 parent f2a02e1 commit 9942fe5

File tree

6 files changed

+18
-10
lines changed

6 files changed

+18
-10
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<Button text="Stop listening" (tap)="firestoreStopListening()" class="button"></Button>
2828
<Button text="Where (by reference)" (tap)="firestoreWhere()" class="button"></Button>
2929
<Button text="Where, Order, Limit" (tap)="firestoreWhereOrderLimit()" class="button"></Button>
30-
<Button text="Where array_contains" (tap)="firestoreWhereCityHasALake()" class="button"></Button>
30+
<Button text="Where array_contains" (tap)="firestoreWhereCityHasALakeAndOrMountain()" class="button"></Button>
3131
<Button text="Start at 'LA'" (tap)="firestoreStartAt()" class="button"></Button>
3232
<Button text="Start after 'LA'" (tap)="firestoreStartAfter()" class="button"></Button>
3333
<Button text="Delete" (tap)="firestoreDelete()" class="button"></Button>

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -358,24 +358,25 @@ export class FirestoreComponent {
358358

359359
firestoreWhereOrderLimit(): void {
360360
const query: firestore.Query = firebase.firestore().collection("cities")
361-
.where("state", "==", "CA")
361+
// .where("state", "==", "CA")
362+
.where("state", "in", ["CA", "WA"])
362363
.where("population", "<", 99999999)
363364
.orderBy("population", "desc")
364-
.limit(2);
365+
.limit(4);
365366

366367
query
367368
.get()
368369
.then((querySnapshot: firestore.QuerySnapshot) => {
369370
querySnapshot.forEach(doc => {
370-
console.log(`Large Californian city: ${doc.id} => ${JSON.stringify(doc.data())}`);
371+
console.log(`Large CA/WA city: ${doc.id} => ${JSON.stringify(doc.data())}`);
371372
});
372373
})
373374
.catch(err => console.log("firestoreWhereOrderLimit failed, error: " + err));
374375
}
375376

376-
firestoreWhereCityHasALake(): void {
377+
firestoreWhereCityHasALakeAndOrMountain(): void {
377378
const query: firestore.Query = firebase.firestore().collection("cities")
378-
.where("landmarks", "array-contains", "lake");
379+
.where("landmarks", "array-contains-any", ["mountain", "lake"]);
379380

380381
query
381382
.get()

docs/FIRESTORE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ sanFranciscoDocument.update({
236236
### `collection.where()`
237237
Firestore supports advanced querying with the `where` function. Those `where` clauses can be chained to form logical 'AND' queries:
238238

239-
You can use the operators defined in `firestore.WhereFilterOp`, which are: `'<' | '<=' | '==' | '>=' | '>' | 'array-contains'`.
239+
You can use the operators defined in `firestore.WhereFilterOp`, which are: `'<' | '<=' | '==' | '>=' | '>' | 'in' | 'array-contains' | 'array-contains-any'`.
240240

241241
```typescript
242242
const citiesCollection = firebase.firestore().collection("cities");

src/firebase.android.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -2602,9 +2602,12 @@ firebase.firestore.where = (collectionPath: string, fieldPath: string, opStr: fi
26022602
query = query.whereGreaterThanOrEqualTo(fieldPath, firebase.toValue(value));
26032603
} else if (opStr === ">") {
26042604
query = query.whereGreaterThan(fieldPath, firebase.toValue(value));
2605+
} else if (opStr === "in") {
2606+
query = query.whereIn(fieldPath, firebase.toValue(value));
26052607
} else if (opStr === "array-contains") {
2606-
// TODO remove 'any' when typings have been updated
2607-
query = (<any>query).whereArrayContains(fieldPath, firebase.toValue(value));
2608+
query = query.whereArrayContains(fieldPath, firebase.toValue(value));
2609+
} else if (opStr === "array-contains-any") {
2610+
query = query.whereArrayContainsAny(fieldPath, firebase.toValue(value));
26082611
} else {
26092612
console.log("Illegal argument for opStr: " + opStr);
26102613
return null;

src/firebase.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ export namespace dynamicLinks {
668668

669669
export namespace firestore {
670670
export type DocumentData = { [field: string]: any };
671-
export type WhereFilterOp = '<' | '<=' | '==' | '>=' | '>' | 'array-contains';
671+
export type WhereFilterOp = '<' | '<=' | '==' | '>=' | '>' | 'in' | 'array-contains' | 'array-contains-any';
672672
export type OrderByDirection = 'desc' | 'asc';
673673

674674
export interface GeoPoint {

src/firebase.ios.ts

+4
Original file line numberDiff line numberDiff line change
@@ -2261,6 +2261,10 @@ firebase.firestore.where = (collectionPath: string, fieldPath: string, opStr: fi
22612261
query = query.queryWhereFieldIsGreaterThan(fieldPath, value);
22622262
} else if (opStr === "array-contains") {
22632263
query = query.queryWhereFieldArrayContains(fieldPath, value);
2264+
} else if (opStr === "array-contains-any") {
2265+
query = query.queryWhereFieldArrayContainsAny(fieldPath, value);
2266+
} else if (opStr === "in") {
2267+
query = query.queryWhereFieldIn(fieldPath, value);
22642268
} else {
22652269
console.log("Illegal argument for opStr: " + opStr);
22662270
return null;

0 commit comments

Comments
 (0)