From 183ce4dbd63fe35306bc5f5b10f7ddf5166ac0ab Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Thu, 2 Mar 2023 16:07:27 -0800 Subject: [PATCH 1/4] Add OR snippets --- .../google/example/firestore/DocSnippets.java | 88 +++++++++++++++++++ .../example/firestore/kotlin/DocSnippets.kt | 88 +++++++++++++++++++ 2 files changed, 176 insertions(+) diff --git a/firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java b/firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java index 37cf8266a..b57e2bb3b 100644 --- a/firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java +++ b/firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java @@ -1335,4 +1335,92 @@ public void onComplete(@NonNull Task task) { }); // [END count_aggregate_query] } + + public void orQuery() { + CollectionReference collection = db.collection("cities"); + // [START or_queries] + Query query = collection.where(Filter.and( + Filter.greaterThan("name", "L"), + Filter.or( + Filter.equalTo("capital", true), + Filter.greaterThanOrEqualTo("population", 1000000) + ) + )); + // [END or_queries] + } + + public void orQueryDisjunctions() { + CollectionReference collection = db.collection("cities"); + + // [START one_disjunction] + collection.whereEqualTo("a", 1); + // [END one_disjunction] + + // [START two_disjunctions] + collection.where(Filter.or( + Filter.equalTo("a", 1), + Filter.equalTo("b", 2) + )); + // [END two_disjunctions] + + // [START four_disjunctions] + collection.where(Filter.or( + Filter.and( + Filter.equalTo("a", 1), + Filter.equalTo("c", 3) + ), + Filter.and( + Filter.equalTo("a", 1), + Filter.equalTo("d", 4) + ), + Filter.and( + Filter.equalTo("b", 2), + Filter.equalTo("c", 3) + ), + Filter.and( + Filter.equalTo("b", 2), + Filter.equalTo("d", 4) + ) + )); + // [END four_disjunctions] + + // [START four_disjunctions_compact] + collection.where(Filter.and( + Filter.or( + Filter.equalTo("a", 1), + Filter.equalTo("b", 2) + ), + Filter.or( + Filter.equalTo("c", 3), + Filter.equalTo("d", 4) + ) + )); + // [END four_disjunctions_compact] + + // [START 20_disjunctions] + collection.where(Filter.or( + Filter.inArray("a", Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), + Filter.inArray("b", Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), + )); + // [END 20_disjunctions] + + // [START 10_disjunctions] + collection.where(Filter.and( + Filter.inArray("a", Arrays.asList(1, 2, 3, 4, 5)), + Filter.or( + Filter.equalTo("b", 2), + Filter.equalTo("c", 3) + ) + )); + // [END 10_disjunctions] + } + + public void illegalDisjunctions() { + // [START 50_disjunctions] + collection.where(Filter.and( + Filter.inArray("a", Arrays.asList(1, 2, 3, 4, 5)), + Filter.inArray("b", Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), + )); + // [END 50_disjunctions] + } } diff --git a/firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt b/firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt index 1be9d8322..a58c1e215 100644 --- a/firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt +++ b/firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt @@ -1107,4 +1107,92 @@ abstract class DocSnippets(val db: FirebaseFirestore) { } // [END count_aggregate_query] } + + fun orQuery() { + val collection = db.collection("cities") + // [START or_query] + val query = collection.where(Filter.and( + Filter.greaterThan("name", "L"), + Filter.or( + Filter.equalTo("capital", true), + Filter.greaterThanOrEqualTo("population", 1000000) + )) + // [END or_query] + } + + fun orQueryDisjunctions() { + val collection = db.collection("cities") + + // [START one_disjunction] + collection.whereEqualTo("a", 1) + // [END one_disjunction] + + // [START two_disjunctions] + collection.where(Filter.or( + Filter.equalTo("a", 1), + Filter.equalTo("b", 2) + )) + // [END two_disjunctions] + + // [START four_disjunctions] + collection.where(Filter.or( + Filter.and( + Filter.equalTo("a", 1), + Filter.equalTo("c", 3) + ), + Filter.and( + Filter.equalTo("a", 1), + Filter.equalTo("d", 4) + ), + Filter.and( + Filter.equalTo("b", 2), + Filter.equalTo("c", 3) + ), + Filter.and( + Filter.equalTo("b", 2), + Filter.equalTo("d", 4) + ) + )) + // [END four_disjunctions] + + // [START four_disjunctions_compact] + collection.where(Filter.and( + Filter.or( + Filter.equalTo("a", 1), + Filter.equalTo("b", 2) + ), + Filter.or( + Filter.equalTo("c", 3), + Filter.equalTo("d", 4) + ) + )) + // [END four_disjunctions_compact] + + // [START 20_disjunctions] + collection.where(Filter.or( + Filter.inArray("a", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), + Filter.inArray("b", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), + )) + // [END 20_disjunctions] + + // [START 10_disjunctions] + collection.where(Filter.and( + Filter.inArray("a", [1, 2, 3, 4, 5]), + Filter.or( + Filter.equalTo("b", 2), + Filter.equalTo("c", 3) + ) + )) + // [END 10_disjunctions] + } + + fun illegalDisjunctions() { + val collection = db.collection("cities") + // [START 50_disjunctions] + collection.where(Filter.and( + Filter.inArray("a", [1, 2, 3, 4, 5]), + Filter.inArray("b", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), + )); + // [END 50_disjunctions] + } } From 3a2fc21b7bf1a0d6d2fe48571de507e2c249b1f4 Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Tue, 21 Mar 2023 16:49:21 -0400 Subject: [PATCH 2/4] fix bad query --- .../main/java/com/google/example/firestore/DocSnippets.java | 2 +- .../java/com/google/example/firestore/kotlin/DocSnippets.kt | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java b/firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java index b57e2bb3b..672030c5e 100644 --- a/firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java +++ b/firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java @@ -1340,7 +1340,7 @@ public void orQuery() { CollectionReference collection = db.collection("cities"); // [START or_queries] Query query = collection.where(Filter.and( - Filter.greaterThan("name", "L"), + Filter.equalTo("state", "CA"), Filter.or( Filter.equalTo("capital", true), Filter.greaterThanOrEqualTo("population", 1000000) diff --git a/firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt b/firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt index a58c1e215..5bcf88be8 100644 --- a/firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt +++ b/firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt @@ -1112,10 +1112,11 @@ abstract class DocSnippets(val db: FirebaseFirestore) { val collection = db.collection("cities") // [START or_query] val query = collection.where(Filter.and( - Filter.greaterThan("name", "L"), + Filter.equalTo("state", "CA"), Filter.or( Filter.equalTo("capital", true), Filter.greaterThanOrEqualTo("population", 1000000) + ) )) // [END or_query] } From b114e8c3ef92b9f8550c81ce0d74ae485c90dbac Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Thu, 30 Oct 2025 15:55:08 -0700 Subject: [PATCH 3/4] fix mismatched name --- .../java/com/google/example/firestore/kotlin/DocSnippets.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt b/firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt index 1d26088f2..fa503b89c 100644 --- a/firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt +++ b/firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt @@ -1144,7 +1144,7 @@ abstract class DocSnippets(val db: FirebaseFirestore) { fun orQuery() { val collection = db.collection("cities") - // [START or_query] + // [START or_queries] val query = collection.where(Filter.and( Filter.equalTo("state", "CA"), Filter.or( @@ -1152,7 +1152,7 @@ abstract class DocSnippets(val db: FirebaseFirestore) { Filter.greaterThanOrEqualTo("population", 1000000) ) )) - // [END or_query] + // [END or_queries] } fun orQueryDisjunctions() { From fcd4e442a4465e8cc2234ba70fb0c94ed4c02549 Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Thu, 30 Oct 2025 16:25:09 -0700 Subject: [PATCH 4/4] update Firestore and fix build errors --- firestore/app/build.gradle.kts | 2 +- .../com/google/example/firestore/DocSnippets.java | 6 ++++-- .../google/example/firestore/kotlin/DocSnippets.kt | 11 ++++++----- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/firestore/app/build.gradle.kts b/firestore/app/build.gradle.kts index 0fc4c0bc0..645b2075d 100644 --- a/firestore/app/build.gradle.kts +++ b/firestore/app/build.gradle.kts @@ -42,7 +42,7 @@ dependencies { implementation("androidx.multidex:multidex:2.0.1") // Import the BoM for the Firebase platform - implementation(platform("com.google.firebase:firebase-bom:34.4.0")) + implementation(platform("com.google.firebase:firebase-bom:34.5.0")) // Declare the dependency for the Cloud Firestore library // When using the BoM, you don't specify versions in Firebase library dependencies diff --git a/firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java b/firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java index a24f7c300..1e1799724 100644 --- a/firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java +++ b/firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java @@ -24,6 +24,7 @@ import com.google.firebase.firestore.EventListener; import com.google.firebase.firestore.FieldPath; import com.google.firebase.firestore.FieldValue; +import com.google.firebase.firestore.Filter; import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.FirebaseFirestoreException; import com.google.firebase.firestore.FirebaseFirestoreSettings; @@ -1434,7 +1435,7 @@ public void orQueryDisjunctions() { // [START 20_disjunctions] collection.where(Filter.or( Filter.inArray("a", Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), - Filter.inArray("b", Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), + Filter.inArray("b", Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) )); // [END 20_disjunctions] @@ -1450,10 +1451,11 @@ public void orQueryDisjunctions() { } public void illegalDisjunctions() { + CollectionReference collection = db.collection("cities"); // [START 50_disjunctions] collection.where(Filter.and( Filter.inArray("a", Arrays.asList(1, 2, 3, 4, 5)), - Filter.inArray("b", Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), + Filter.inArray("b", Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) )); // [END 50_disjunctions] } diff --git a/firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt b/firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt index fa503b89c..de23a75ef 100644 --- a/firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt +++ b/firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt @@ -8,6 +8,7 @@ import com.google.firebase.firestore.AggregateField import com.google.firebase.firestore.AggregateSource import com.google.firebase.firestore.DocumentChange import com.google.firebase.firestore.FieldValue +import com.google.firebase.firestore.Filter import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.FirebaseFirestoreException import com.google.firebase.firestore.MetadataChanges @@ -1205,14 +1206,14 @@ abstract class DocSnippets(val db: FirebaseFirestore) { // [START 20_disjunctions] collection.where(Filter.or( - Filter.inArray("a", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), - Filter.inArray("b", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), + Filter.inArray("a", listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), + Filter.inArray("b", listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), )) // [END 20_disjunctions] // [START 10_disjunctions] collection.where(Filter.and( - Filter.inArray("a", [1, 2, 3, 4, 5]), + Filter.inArray("a", listOf(1, 2, 3, 4, 5)), Filter.or( Filter.equalTo("b", 2), Filter.equalTo("c", 3) @@ -1225,8 +1226,8 @@ abstract class DocSnippets(val db: FirebaseFirestore) { val collection = db.collection("cities") // [START 50_disjunctions] collection.where(Filter.and( - Filter.inArray("a", [1, 2, 3, 4, 5]), - Filter.inArray("b", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), + Filter.inArray("a", listOf(1, 2, 3, 4, 5)), + Filter.inArray("b", listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), )); // [END 50_disjunctions] }