Operators
Operators allow you to control how you query your documents. You can use these operators to filter out documents.
We can use these operators in conjunction with the where clause or the having clause:
query({
from({
posts: {
where(["created_at", ">=", "2022"]);
};
});
});In the example above, we're using the >= operator to fetch all posts that were created in 2022 or after that.
Comparison Operators
Equal To
To check if a value is matches specified value, you can use the =, == or === operators:
query({
from({
users: {
where(["first_name", "==", "Donald"]);
};
});
});This will return all the user documents where the first_name is "Donald".
| Option | Type | Description |
|---|---|---|
| = | string | number | Equal to (type safe). |
| === | string | number | Equal to (type safe). |
| == | string | number | Equal to (type unsafe). |
Not Equal To
To check if a value does not match specified value, you can use the !=, !== or <> operators:
query({
from({
users: {
where(["city", "!==", "Cape Town"]);
};
});
});This will return all user documents where the city is not "Cape Town".
| Option | Type | Description |
|---|---|---|
| != | string | number | Not equal to (type safe). |
| !== | string | number | Not equal to (type safe). |
| <> | string | number | Not equal to (type unsafe). |
Geater Than
To check if a value is greater than another value, you can use the > or >= operators:
query({
from({
users: {
where(["age", ">", 17]);
};
});
});This will return all user documents where the age is greater than "17".
| Option | Type | Description |
|---|---|---|
| > | number | timestamp | Greater than. |
| >= | string | number | Greater than or equals to. |
Less Than
To check if a value is less than another value, you can use the < or <= operators:
query({
from({
users: {
where(["created_at", "<=", 2020]);
};
});
});This will return all user documents that where created before "2021".
| Option | Type | Description |
|---|---|---|
| < | number | timestamp | Less than. |
| <= | number | timestamp | Less than or equals to. |
Logical Operators
Like
To check if a value partially matches another value, you can use the LIKE, or NOT LIKE operators:
query({
from({
cities: {
where(["name", "LIKE", "C%"]);
};
});
});This will return all city documents where the name starts with the letter "C".
| Option | Type | Description |
|---|---|---|
| LIKE | string | number | Where value is like. |
| NOT LIKE | string | number | Where value is not like. |
In
To check if a value matches a list of values, you can use the IN, or NOT IN operators:
query({
from({
users: {
where(["name", "IN", ["Donald", "Luna"]]);
};
});
});This will return all user documents where the name is either "Donald" or "Luna".
| Option | Type | Description |
|---|---|---|
| IN | string[] | number[] | Where value is in a list. |
| NOT IN | string[] | number[] | Where value is not in a list. |
Between
To check if a value is in between 2 values, you can use the BETWEEN, or NOT BETWEEN operators:
query({
from({
users: {
where(["age", "BETWEEN", [17, 26]]);
};
});
});This will return all user documents where the age is between "17" and "26".
| Option | Type | Description |
|---|---|---|
| BETWEEN | number[] | Where value is between a list. |
| NOT BETWEEN | number[] | Where value is not between a list. |