Skip to content

Commit

Permalink
Add OR snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
morganchen12 committed Mar 3, 2023
1 parent 50daa28 commit 0e53a82
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1399,4 +1399,92 @@ public void onComplete(@NonNull Task<AggregateQuerySnapshot> 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]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1164,4 +1164,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]
}
}

0 comments on commit 0e53a82

Please sign in to comment.