FilterQuery
DSL for MongoDB operators that are used as predicates in conditions.
Example
This expression type is available in multiple operators, most commonly find
:
class User(
val name: String,
val age: Int,
)
collection.find {
User::age gte 18
}
Beware of arrays!
MongoDB operators do not discriminate between scalars and arrays. When an array is encountered, all operators attempt to match on the array itself. If the match fails, the operators attempt to match array elements.
It is not possible to mimic this behavior in KtMongo while still keeping type-safety, so operators may behave strangely when arrays are encountered.
Note that if the collection corresponds to the declared Kotlin type, these situations can never happen, as the Kotlin type system doesn't allow them to.
When developers attempt to perform an operator on the entire array, they should use operators as normal:
class User(
val name: String,
val favoriteNumbers: List<Int>
)
collection.find {
User::favoriteNumbers eq listOf(1, 2)
}
Developers should use the request above when they want to match a document similar to:
{
favoriteNumbers: [1, 2]
}
The following document will NOT match:
{
favoriteNumbers: [3]
}
However, due to MongoDB's behavior when encountering arrays, it should be noted that the following document WILL match:
{
favoriteNumbers: [
[3],
[1, 2],
[7, 2]
]
}
To execute an operator on one of the elements of an array, see anyValue.
Operators
Comparison query:
Logical query:
Element query:
Array query:
Bitwise query:
Text query:
If you can't find an operator you're searching for, visit the tracking issue.
Functions
Performs a logical AND
operation on one or more expressions, and selects the documents that satisfy all the expressions.
Specify multiple operators on fields of a single array element.
Specify multiple operators on a single array element.
Matches documents where all bit positions present in mask are clear (i.e., 0) in the current field.
Matches documents where all bit positions present in mask are set (i.e., 1) in the current field.
Matches documents where any bit position present in mask is clear (i.e., 0) in the current field.
Matches documents where any bit position present in mask is set (i.e., 1) in the current field.
Selects documents where the value of a field is an array that contains all the specified values.
Matches documents that do not contain the specified field. Documents where the field if null
are not matched.
Enables the usage of aggregation values within a regular query.
Refers to a specific item in an array, by its index.
Refers to a specific item in a map, by its name.
Selects documents for which this field has a value greater or equal to value.
Targets a single field to execute a targeted predicate.
Matches documents in which an array is empty or absent.
Selects documents in which this field has a value included in range.
Matches documents in which a map is empty or absent.
Matches documents in which a map is not empty.
Matches documents in which an array is not empty.
Selects documents for which this field is not equal to any of the given values.
Selects documents for which the field is not undefined
.
Selects documents for which the field is undefined
.
Selects documents for which this field has a value lesser or equal to value.
Performs a logical NOR
operation on one or more expressions, and selects the documents that do not satisfy any of the expressions.
Performs a logical NOT
operation on the specified expression and selects the documents that do not match the expression. This includes the elements that do not contain the field.
Performs a logical OR
operation on one or more expressions, and selects the documents that satisfy at least one of the expressions.
Matches documents where the field corresponds to a given regex expression.
Selects documents where the value of a field is an array of size size (exactly).
Writes the result of simplifying to a new Bson.
Writes the result of simplifying this expression into writer.