Operators
Monarch ORM provides a set of utility operators to simplify the construction of complex query filters. These operators are type-safe and designed to work seamlessly with your schema definitions. They offer a more concise and readable way to express common query conditions.
Available Operators
Logical Operators
These operators combine multiple expressions to create more complex query logic.
-
and(...expressions): Returns documents that satisfy all the specified expressions. -
or(...expressions): Returns documents that satisfy at least one of the specified expressions. -
nor(...expressions): Returns documents that fail to satisfy all the specified expressions.
Comparison Operators
These operators compare a field's value to a specified value.
-
eq(value): Matches values that are equal to the specified value. -
neq(value): Matches values that are not equal to the specified value. -
gt(value): Matches values that are greater than the specified value. -
lt(value): Matches values that are less than the specified value. -
gte(value): Matches values that are greater than or equal to the specified value. -
lte(value): Matches values that are less than or equal to the specified value.
Array Operators
These operators check for membership within an array.
-
inArray(values): Matches any values that exist in the specified array. -
notInArray(values): Matches any values that do not exist in the specified array.
Existence Operators
These operators check if a field exists in the document.
-
exists(): Matches documents that contain the specified field. -
notExists(): Matches documents that do not contain the specified field.
Size Operator
These operators check for array sizes in the document.
-
size(value): Matches documents where the array has the specified size.
Usage Guidelines
-
Import Statements: Make sure to import the operators from
"monarch-orm/operators"or"monarch-orm"correctly. -
Type Safety: These operators are designed to work with your schema types. Using them with incorrect types might lead to TypeScript errors or unexpected behavior.
-
Readability: Using these operators can significantly improve the readability of your query filters.
-
Equivalence: For each Monarch ORM operator, the equivalent MongoDB query syntax is provided for clarity and reference.