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 eb54dc207..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; @@ -1370,6 +1371,95 @@ 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.equalTo("state", "CA"), + 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() { + 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)) + )); + // [END 50_disjunctions] + } + public void sumAggregateCollection() { // [START sum_aggregate_collection] Query query = db.collection("cities"); 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 0a12d177b..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 @@ -1142,6 +1143,95 @@ abstract class DocSnippets(val db: FirebaseFirestore) { // [END count_aggregate_query] } + fun orQuery() { + val collection = db.collection("cities") + // [START or_queries] + val query = collection.where(Filter.and( + Filter.equalTo("state", "CA"), + Filter.or( + Filter.equalTo("capital", true), + Filter.greaterThanOrEqualTo("population", 1000000) + ) + )) + // [END or_queries] + } + + 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", 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", listOf(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", listOf(1, 2, 3, 4, 5)), + Filter.inArray("b", listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), + )); + // [END 50_disjunctions] + } + fun sumAggregateCollection() { // [START sum_aggregate_collection] val query = db.collection("cities")