Comparison functions | Firestore | Firebase
Comparison functions
| Name | Description |
EQUAL |
Equality comparison |
GREATER_THAN |
Greater than comparison |
GREATER_THAN_OR_EQUAL |
Greater than or equal comparison |
LESS_THAN |
Less than comparison |
LESS_THAN_OR_EQUAL |
Less than or equal comparison |
NOT_EQUAL |
Not equals comparison |
CMP |
General comparison |
EQUAL
Syntax:
equal(x: ANY, y: ANY) -> BOOLEAN
Examples:
x |
y |
equal(x, y) |
|---|---|---|
| 1L | 1L | TRUE |
| 1.0 | 1L | TRUE |
| -1.0 | 1L | FALSE |
| NaN | NaN | TRUE |
NULL |
NULL |
TRUE |
NULL |
ABSENT |
FALSE |
Description:
Returns TRUE if x and y are equal, and FALSE otherwise.
const result = await db.pipeline()
.collection("books")
.select(field("rating").equal(5).as("hasPerfectRating"))
.execute();
test.firestore.js
GREATER_THAN
Syntax:
greater_than(x: ANY, y: ANY) -> BOOLEAN
Description:
Returns TRUE if x is greater than y, and FALSE otherwise.
If x and y are not comparable, returns FALSE.
Examples:
x |
y |
greater_than(x, y) |
|---|---|---|
| 1L | 0.0 | TRUE |
| 1L | 1L | FALSE |
| 1L | 2L | FALSE |
| "foo" | 0L | FALSE |
| 0L | "foo" | FALSE |
| NaN | 0L | FALSE |
| 0L | NaN | FALSE |
NULL |
NULL |
FALSE |
GREATER_THAN_OR_EQUAL
Syntax:
greater_than_or_equal(x: ANY, y: ANY) -> BOOLEAN
Description:
Returns TRUE if x is greater than or equal to y, and FALSE otherwise.
If x and y are not comparable, returns FALSE.
Examples:
x |
y |
greater_than_or_equal(x, y) |
|---|---|---|
| 1L | 0.0 | TRUE |
| 1L | 1L | TRUE |
| 1L | 2L | FALSE |
| "foo" | 0L | FALSE |
| 0L | "foo" | FALSE |
| NaN | 0L | FALSE |
| 0L | NaN | FALSE |
NULL |
NULL |
TRUE |
LESS_THAN
Syntax:
less_than(x: ANY, y: ANY) -> BOOLEAN
Description:
Returns TRUE if x is less than y, and FALSE otherwise.
If x and y are not comparable, returns FALSE.
Examples:
x |
y |
less_than(x, y) |
|---|---|---|
| 1L | 0.0 | FALSE |
| 1L | 1L | FALSE |
| 1L | 2L | TRUE |
| "foo" | 0L | FALSE |
| 0L | "foo" | FALSE |
| NaN | 0L | FALSE |
| 0L | NaN | FALSE |
NULL |
NULL |
FALSE |
LESS_THAN_OR_EQUAL
Syntax:
less_than_or_equal(x: ANY, y: ANY) -> BOOLEAN
Description:
Returns TRUE if x is less than or equal to y, and FALSE otherwise.
If x and y are not comparable, returns FALSE.
Examples:
x |
y |
less_than(x, y) |
|---|---|---|
| 1L | 0.0 | FALSE |
| 1L | 1L | TRUE |
| 1L | 2L | TRUE |
| "foo" | 0L | FALSE |
| 0L | "foo" | FALSE |
| NaN | 0L | FALSE |
| 0L | NaN | FALSE |
NULL |
NULL |
TRUE |
NOT_EQUAL
Syntax:
not_equal(x: ANY, y: ANY) -> BOOLEAN
Description:
Returns TRUE if x is not equal to y, and FALSE otherwise.
Examples:
x |
y |
not_equal(x, y) |
|---|---|---|
| 1L | 1L | FALSE |
| 1.0 | 1L | FALSE |
| -1.0 | 1L | TRUE |
| NaN | 0L | TRUE |
| NaN | NaN | FALSE |
NULL |
NULL |
FALSE |
NULL |
ABSENT |
TRUE |
CMP
Syntax:
cmp(x: ANY, y: ANY) -> Int64
Description:
Compares x & y, returning:
1Lifxis greater thany.-1Lifxis less thany.0Lotherwise.
Unlike other comparison functions, the cmp(...) function works across types,
following the same ordering used in the sort(...) stage. See
value type order for how values are ordered across types.
Examples:
x |
y |
cmp(x, y) |
|---|---|---|
| 1L | 1L | 0L |
| 1.0 | 1L | 0L |
| -1.0 | 1L | -1L |
| 42.5D | "foo" | -1L |
NULL |
NULL |
0L |
NULL |
ABSENT |
0L |