MongoDB request DSL • opensavvy.ktmongo.dsl.query • FilterQuery
FilterQuery¶
interface FilterQuery<T> : CompoundBsonNode, FieldDsl
DSL for MongoDB operators that are used as predicates in conditions.
Example¶
This expression type is available in multiple operators, most commonly find
:
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:
The following document will NOT match:
However, due to MongoDB's behavior when encountering arrays, it should be noted that the following document WILL match:
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.
Properties¶
any
¶
open val <V> KProperty1<T, Collection<V>>.any: Field<T, V>
Specify operators on array elements.
Specify operators on array elements.
context
¶
abstract val context:
The context used to generate this expression.
field
¶
Converts a Kotlin property into a Field
.
Functions¶
accept
¶
Adds a new node
as a child of this one.
acceptAll
¶
Adds any number of nodes
into this one.
and
¶
abstract fun and(block: FilterQuery<T>.() -> Unit)
Performs a logical AND
operation on one or more expressions, and selects the documents that satisfy all the expressions.
any
¶
open fun <V> KProperty1<T, Collection<V>>.any(block: FilterQuery<V>.() -> Unit)
Specify multiple operators on fields of a single array element.
abstract fun <V> Field<T, Collection<V>>.any(block: FilterQuery<V>.() -> Unit)
Specify multiple operators on fields of a single array element.
anyValue
¶
open fun <V> KProperty1<T, Collection<V>>.anyValue(block: FilterQueryPredicate<V>.() -> Unit)
Specify multiple operators on a single array element.
abstract fun <V> Field<T, Collection<V>>.anyValue(block: FilterQueryPredicate<V>.() -> Unit)
Specify multiple operators on a single array element.
bitsAllClear
¶
open infix fun KProperty1<T, *>.bitsAllClear(mask: )
Matches documents where all bit positions present in mask
are clear (i.e., 0) in the current field.
open infix fun KProperty1<T, *>.bitsAllClear(mask: ByteArray)
Matches documents where all bit positions present in mask
are clear (i.e., 0) in the current field.
open infix fun Field<T, *>.bitsAllClear(mask: )
Matches documents where all bit positions present in mask
are clear (i.e., 0) in the current field.
open infix fun Field<T, *>.bitsAllClear(mask: ByteArray)
Matches documents where all bit positions present in mask
are clear (i.e., 0) in the current field.
bitsAllSet
¶
open infix fun KProperty1<T, *>.bitsAllSet(mask: )
Matches documents where all bit positions present in mask
are set (i.e., 1) in the current field.
open infix fun KProperty1<T, *>.bitsAllSet(mask: ByteArray)
Matches documents where all bit positions present in mask
are set (i.e., 1) in the current field.
open infix fun Field<T, *>.bitsAllSet(mask: )
Matches documents where all bit positions present in mask
are set (i.e., 1) in the current field.
open infix fun Field<T, *>.bitsAllSet(mask: ByteArray)
Matches documents where all bit positions present in mask
are set (i.e., 1) in the current field.
bitsAnyClear
¶
open infix fun KProperty1<T, *>.bitsAnyClear(mask: )
Matches documents where any bit position present in mask
is clear (i.e., 0) in the current field.
open infix fun KProperty1<T, *>.bitsAnyClear(mask: ByteArray)
Matches documents where any bit position present in mask
is clear (i.e., 0) in the current field.
open infix fun Field<T, *>.bitsAnyClear(mask: )
Matches documents where any bit position present in mask
is clear (i.e., 0) in the current field.
open infix fun Field<T, *>.bitsAnyClear(mask: ByteArray)
Matches documents where any bit position present in mask
is clear (i.e., 0) in the current field.
bitsAnySet
¶
open infix fun KProperty1<T, *>.bitsAnySet(mask: )
Matches documents where any bit position present in mask
is set (i.e., 1) in the current field.
open infix fun KProperty1<T, *>.bitsAnySet(mask: ByteArray)
Matches documents where any bit position present in mask
is set (i.e., 1) in the current field.
open infix fun Field<T, *>.bitsAnySet(mask: )
Matches documents where any bit position present in mask
is set (i.e., 1) in the current field.
open infix fun Field<T, *>.bitsAnySet(mask: ByteArray)
Matches documents where any bit position present in mask
is set (i.e., 1) in the current field.
containsAll
¶
open infix fun <V> KProperty1<T, Collection<V>>.containsAll(values: Collection<V>)
Selects documents where the value of a field is an array that contains all the specified values
.
abstract infix fun <V> Field<T, Collection<V>>.containsAll(values: Collection<V>)
Selects documents where the value of a field is an array that contains all the specified values
.
div
¶
Refers to child
as a nested field of the current field.
open operator fun <V, V2> KProperty1<T, Collection<V>>.div(other: KProperty1<V, V2>): Field<T, V2>
Combines Kotlin properties into a path usable to point to any item in an array.
Combines Kotlin properties into a path usable to point to any item in an array.
Combines Kotlin properties into a path usable to point to any item in an array.
Combines Kotlin properties into a path usable to point to any item in an array.
doesNotExist
¶
open fun KProperty1<T, *>.doesNotExist()
Matches documents that do not contain the specified field. Documents where the field if null
are not matched.
open fun Field<T, *>.doesNotExist()
Matches documents that do not contain the specified field. Documents where the field if null
are not matched.
eq
¶
Matches documents where the value of a field equals the value
.
Matches documents where the value of a field equals the value
.
eqNotNull
¶
Matches documents where the value of a field equals value
.
Matches documents where the value of a field equals value
.
exists
¶
open fun KProperty1<T, *>.exists()
Matches documents that contain the specified field, including values where the field value is null
.
Matches documents that contain the specified field, including values where the field value is null
.
expr
¶
Enables the usage of aggregation values
within a regular query.
freeze
¶
abstract override fun freeze()
Makes this expression immutable.
get
¶
Refers to a specific item in an array, by its index.
Refers to a specific item in a map, by its name.
gt
¶
Selects documents for which this field has a value strictly greater than value
.
Selects documents for which this field has a value strictly greater than value
.
gte
¶
Selects documents for which this field has a value greater or equal to value
.
Selects documents for which this field has a value greater or equal to value
.
gteNotNull
¶
open infix fun <V> KProperty1<T, V>.gteNotNull(value: V?)
Selects documents for which this field has a value greater or equal to value
.
Selects documents for which this field has a value greater or equal to value
.
gtNotNull
¶
Selects documents for which this field has a value strictly greater than value
.
Selects documents for which this field has a value strictly greater than value
.
hasType
¶
open infix fun KProperty1<T, *>.hasType(type: )
Selects documents where the value of the field is an instance of the specified BSON type
.
Selects documents where the value of the field is an instance of the specified BSON type
.
invoke
¶
open operator fun <V> KProperty1<T, V>.invoke(block: FilterQueryPredicate<V>.() -> Unit)
Targets a single field to execute a targeted predicate
.
Targets a single field to execute a targeted predicate
.
isEmpty
¶
open fun KProperty1<T, Collection<*>>.isEmpty()
Matches documents in which an array is empty or absent.
open fun Field<T, Collection<*>>.isEmpty()
Matches documents in which an array is empty or absent.
isIn
¶
open infix fun <V : Comparable<V>, R : ClosedRange<V>, OpenEndRange<V>> KProperty1<T, V?>.isIn(range: R)
Selects documents in which this field has a value included in range
.
open infix fun <V : Comparable<V>> KProperty1<T, V?>.isIn(range: ClosedRange<V>)
Selects documents in which this field has a value included in range
.
open infix fun <V : Comparable<V>> KProperty1<T, V?>.isIn(range: OpenEndRange<V>)
Selects documents in which this field has a value included in range
.
open infix fun <V : Comparable<V>, R : ClosedRange<V>, OpenEndRange<V>> Field<T, V?>.isIn(range: R)
Selects documents in which this field has a value included in range
.
open infix fun <V : Comparable<V>> Field<T, V?>.isIn(range: ClosedRange<V>)
Selects documents in which this field has a value included in range
.
open infix fun <V : Comparable<V>> Field<T, V?>.isIn(range: OpenEndRange<V>)
Selects documents in which this field has a value included in range
.
isMapEmpty
¶
open fun KProperty1<T, Map<String, *>>.isMapEmpty()
Matches documents in which a map is empty or absent.
open fun Field<T, Map<String, *>>.isMapEmpty()
Matches documents in which a map is empty or absent.
isMapNotEmpty
¶
open fun KProperty1<T, Map<String, *>>.isMapNotEmpty()
Matches documents in which a map is not empty.
open fun Field<T, Map<String, *>>.isMapNotEmpty()
Matches documents in which a map is not empty.
isNotEmpty
¶
open fun KProperty1<T, Collection<*>>.isNotEmpty()
Matches documents in which an array is not empty.
open fun Field<T, Collection<*>>.isNotEmpty()
Matches documents in which an array is not empty.
isNotNull
¶
open fun KProperty1<T, *>.isNotNull()
Selects documents for which the field is not null
.
Selects documents for which the field is not null
.
isNotOneOf
¶
open fun <V> KProperty1<T, V>.isNotOneOf(vararg values: V)
Selects documents for which this field is not equal to any of the given values
.
open fun <V> KProperty1<T, V>.isNotOneOf(values: List<V>)
Selects documents for which this field is not equal to any of the given values
.
Selects documents for which this field is not equal to any of the given values
.
Selects documents for which this field is not equal to any of the given values
.
isNotUndefined
¶
open fun KProperty1<T, *>.isNotUndefined()
Selects documents for which the field is not undefined
.
open fun Field<T, *>.isNotUndefined()
Selects documents for which the field is not undefined
.
isNull
¶
open fun KProperty1<T, *>.isNull()
Selects documents for which the field is null
.
Selects documents for which the field is null
.
isOneOf
¶
Selects documents for which this field is equal to one of the given values
.
Selects documents for which this field is equal to one of the given values
.
Selects documents for which this field is equal to one of the given values
.
Selects documents for which this field is equal to one of the given values
.
isUndefined
¶
open fun KProperty1<T, *>.isUndefined()
Selects documents for which the field is undefined
.
open fun Field<T, *>.isUndefined()
Selects documents for which the field is undefined
.
lt
¶
Selects documents for which this field has a value strictly lesser than value
.
Selects documents for which this field has a value strictly lesser than value
.
lte
¶
Selects documents for which this field has a value lesser or equal to value
.
Selects documents for which this field has a value lesser or equal to value
.
lteNotNull
¶
open infix fun <V> KProperty1<T, V>.lteNotNull(value: V?)
Selects documents for which this field has a value lesser or equal to value
.
Selects documents for which this field has a value lesser or equal to value
.
ltNotNull
¶
Selects documents for which this field has a value strictly lesser than value
.
Selects documents for which this field has a value strictly lesser than value
.
ne
¶
Matches documents where the value of a field does not equal the value
.
Matches documents where the value of a field does not equal the value
.
nor
¶
abstract fun nor(block: FilterQuery<T>.() -> Unit)
Performs a logical NOR
operation on one or more expressions, and selects the documents that do not satisfy any of the expressions.
not
¶
open infix fun <V> KProperty1<T, V>.not(expression: FilterQueryPredicate<V>.() -> Unit)
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 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.
or
¶
abstract fun or(block: FilterQuery<T>.() -> Unit)
Performs a logical OR
operation on one or more expressions, and selects the documents that satisfy at least one of the expressions.
regex
¶
Matches documents where the field corresponds to a given regex expression.
Matches documents where the field corresponds to a given regex expression.
simplify
¶
Returns a simplified (but equivalent) expression to the current expression.
size
¶
open infix fun KProperty1<T, Collection<*>>.size(size: Int)
Selects documents where the value of a field is an array of size size
(exactly).
abstract infix fun Field<T, Collection<*>>.size(size: Int)
Selects documents where the value of a field is an array of size size
(exactly).
toBson
¶
open fun toBson():
Writes the result of simplifying
to a new Bson.
toString
¶
JSON representation of this expression.
writeTo
¶
abstract fun writeTo(writer: )
Writes the result of simplifying
this expression into writer
.