Skip to content

Commit 43ae6b0

Browse files
authored
Merge pull request #430 from firebase/mc/count
Add OR snippets
2 parents 9054a8b + fcd4e44 commit 43ae6b0

File tree

3 files changed

+181
-1
lines changed

3 files changed

+181
-1
lines changed

firestore/app/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ dependencies {
4242
implementation("androidx.multidex:multidex:2.0.1")
4343

4444
// Import the BoM for the Firebase platform
45-
implementation(platform("com.google.firebase:firebase-bom:34.4.0"))
45+
implementation(platform("com.google.firebase:firebase-bom:34.5.0"))
4646

4747
// Declare the dependency for the Cloud Firestore library
4848
// When using the BoM, you don't specify versions in Firebase library dependencies

firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.firebase.firestore.EventListener;
2525
import com.google.firebase.firestore.FieldPath;
2626
import com.google.firebase.firestore.FieldValue;
27+
import com.google.firebase.firestore.Filter;
2728
import com.google.firebase.firestore.FirebaseFirestore;
2829
import com.google.firebase.firestore.FirebaseFirestoreException;
2930
import com.google.firebase.firestore.FirebaseFirestoreSettings;
@@ -1370,6 +1371,95 @@ public void onComplete(@NonNull Task<AggregateQuerySnapshot> task) {
13701371
// [END count_aggregate_query]
13711372
}
13721373

1374+
public void orQuery() {
1375+
CollectionReference collection = db.collection("cities");
1376+
// [START or_queries]
1377+
Query query = collection.where(Filter.and(
1378+
Filter.equalTo("state", "CA"),
1379+
Filter.or(
1380+
Filter.equalTo("capital", true),
1381+
Filter.greaterThanOrEqualTo("population", 1000000)
1382+
)
1383+
));
1384+
// [END or_queries]
1385+
}
1386+
1387+
public void orQueryDisjunctions() {
1388+
CollectionReference collection = db.collection("cities");
1389+
1390+
// [START one_disjunction]
1391+
collection.whereEqualTo("a", 1);
1392+
// [END one_disjunction]
1393+
1394+
// [START two_disjunctions]
1395+
collection.where(Filter.or(
1396+
Filter.equalTo("a", 1),
1397+
Filter.equalTo("b", 2)
1398+
));
1399+
// [END two_disjunctions]
1400+
1401+
// [START four_disjunctions]
1402+
collection.where(Filter.or(
1403+
Filter.and(
1404+
Filter.equalTo("a", 1),
1405+
Filter.equalTo("c", 3)
1406+
),
1407+
Filter.and(
1408+
Filter.equalTo("a", 1),
1409+
Filter.equalTo("d", 4)
1410+
),
1411+
Filter.and(
1412+
Filter.equalTo("b", 2),
1413+
Filter.equalTo("c", 3)
1414+
),
1415+
Filter.and(
1416+
Filter.equalTo("b", 2),
1417+
Filter.equalTo("d", 4)
1418+
)
1419+
));
1420+
// [END four_disjunctions]
1421+
1422+
// [START four_disjunctions_compact]
1423+
collection.where(Filter.and(
1424+
Filter.or(
1425+
Filter.equalTo("a", 1),
1426+
Filter.equalTo("b", 2)
1427+
),
1428+
Filter.or(
1429+
Filter.equalTo("c", 3),
1430+
Filter.equalTo("d", 4)
1431+
)
1432+
));
1433+
// [END four_disjunctions_compact]
1434+
1435+
// [START 20_disjunctions]
1436+
collection.where(Filter.or(
1437+
Filter.inArray("a", Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)),
1438+
Filter.inArray("b", Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
1439+
));
1440+
// [END 20_disjunctions]
1441+
1442+
// [START 10_disjunctions]
1443+
collection.where(Filter.and(
1444+
Filter.inArray("a", Arrays.asList(1, 2, 3, 4, 5)),
1445+
Filter.or(
1446+
Filter.equalTo("b", 2),
1447+
Filter.equalTo("c", 3)
1448+
)
1449+
));
1450+
// [END 10_disjunctions]
1451+
}
1452+
1453+
public void illegalDisjunctions() {
1454+
CollectionReference collection = db.collection("cities");
1455+
// [START 50_disjunctions]
1456+
collection.where(Filter.and(
1457+
Filter.inArray("a", Arrays.asList(1, 2, 3, 4, 5)),
1458+
Filter.inArray("b", Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
1459+
));
1460+
// [END 50_disjunctions]
1461+
}
1462+
13731463
public void sumAggregateCollection() {
13741464
// [START sum_aggregate_collection]
13751465
Query query = db.collection("cities");

firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.google.firebase.firestore.AggregateField
88
import com.google.firebase.firestore.AggregateSource
99
import com.google.firebase.firestore.DocumentChange
1010
import com.google.firebase.firestore.FieldValue
11+
import com.google.firebase.firestore.Filter
1112
import com.google.firebase.firestore.FirebaseFirestore
1213
import com.google.firebase.firestore.FirebaseFirestoreException
1314
import com.google.firebase.firestore.MetadataChanges
@@ -1142,6 +1143,95 @@ abstract class DocSnippets(val db: FirebaseFirestore) {
11421143
// [END count_aggregate_query]
11431144
}
11441145

1146+
fun orQuery() {
1147+
val collection = db.collection("cities")
1148+
// [START or_queries]
1149+
val query = collection.where(Filter.and(
1150+
Filter.equalTo("state", "CA"),
1151+
Filter.or(
1152+
Filter.equalTo("capital", true),
1153+
Filter.greaterThanOrEqualTo("population", 1000000)
1154+
)
1155+
))
1156+
// [END or_queries]
1157+
}
1158+
1159+
fun orQueryDisjunctions() {
1160+
val collection = db.collection("cities")
1161+
1162+
// [START one_disjunction]
1163+
collection.whereEqualTo("a", 1)
1164+
// [END one_disjunction]
1165+
1166+
// [START two_disjunctions]
1167+
collection.where(Filter.or(
1168+
Filter.equalTo("a", 1),
1169+
Filter.equalTo("b", 2)
1170+
))
1171+
// [END two_disjunctions]
1172+
1173+
// [START four_disjunctions]
1174+
collection.where(Filter.or(
1175+
Filter.and(
1176+
Filter.equalTo("a", 1),
1177+
Filter.equalTo("c", 3)
1178+
),
1179+
Filter.and(
1180+
Filter.equalTo("a", 1),
1181+
Filter.equalTo("d", 4)
1182+
),
1183+
Filter.and(
1184+
Filter.equalTo("b", 2),
1185+
Filter.equalTo("c", 3)
1186+
),
1187+
Filter.and(
1188+
Filter.equalTo("b", 2),
1189+
Filter.equalTo("d", 4)
1190+
)
1191+
))
1192+
// [END four_disjunctions]
1193+
1194+
// [START four_disjunctions_compact]
1195+
collection.where(Filter.and(
1196+
Filter.or(
1197+
Filter.equalTo("a", 1),
1198+
Filter.equalTo("b", 2)
1199+
),
1200+
Filter.or(
1201+
Filter.equalTo("c", 3),
1202+
Filter.equalTo("d", 4)
1203+
)
1204+
))
1205+
// [END four_disjunctions_compact]
1206+
1207+
// [START 20_disjunctions]
1208+
collection.where(Filter.or(
1209+
Filter.inArray("a", listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)),
1210+
Filter.inArray("b", listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)),
1211+
))
1212+
// [END 20_disjunctions]
1213+
1214+
// [START 10_disjunctions]
1215+
collection.where(Filter.and(
1216+
Filter.inArray("a", listOf(1, 2, 3, 4, 5)),
1217+
Filter.or(
1218+
Filter.equalTo("b", 2),
1219+
Filter.equalTo("c", 3)
1220+
)
1221+
))
1222+
// [END 10_disjunctions]
1223+
}
1224+
1225+
fun illegalDisjunctions() {
1226+
val collection = db.collection("cities")
1227+
// [START 50_disjunctions]
1228+
collection.where(Filter.and(
1229+
Filter.inArray("a", listOf(1, 2, 3, 4, 5)),
1230+
Filter.inArray("b", listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)),
1231+
));
1232+
// [END 50_disjunctions]
1233+
}
1234+
11451235
fun sumAggregateCollection() {
11461236
// [START sum_aggregate_collection]
11471237
val query = db.collection("cities")

0 commit comments

Comments
 (0)