Skip to content

MongoDB request DSLopensavvy.ktmongo.dsl.queryFilterQuery

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.

Properties

any

open val <V> KProperty1<T, Collection<V>>.any: Field<T, V>

Specify operators on array elements.

open val <V> Field<T, Collection<V>>.any: Field<T, V>

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

abstract override fun accept(node: BsonNode)

Adds a new node as a child of this one.

acceptAll

fun <N : Node> CompoundNode<N>.acceptAll(nodes: Iterable<N>)

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

open operator fun <Root, Parent, Child> KProperty1<Root, Parent>.div(child: KProperty1<Parent & Any, Child>): Field<Root, Child>

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.

open operator fun <V, V2> KProperty1<T, Collection<V>>.div(other: Field<V, V2>): Field<T, V2>

Combines Kotlin properties into a path usable to point to any item in an array.

open operator fun <V, V2> Field<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.

open operator fun <V, V2> Field<T, Collection<V>>.div(other: Field<V, V2>): Field<T, V2>

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

open infix fun <V> KProperty1<T, V>.eq(value: V)

Matches documents where the value of a field equals the value.

open infix fun <V> Field<T, V>.eq(value: V)

Matches documents where the value of a field equals the value.

eqNotNull

open infix fun <V> KProperty1<T, V>.eqNotNull(value: V?)

Matches documents where the value of a field equals value.

open infix fun <V> Field<T, V>.eqNotNull(value: V?)

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.

open fun Field<T, *>.exists()

Matches documents that contain the specified field, including values where the field value is null.

expr

abstract fun expr(block: AggregationOperators.() -> Value<T & Any, Boolean>)

Enables the usage of aggregation values within a regular query.

freeze

abstract override fun freeze()

Makes this expression immutable.

get

open operator fun <Root, Type> KProperty1<Root, Collection<Type>>.get(index: Int): Field<Root, Type>

Refers to a specific item in an array, by its index.

open operator fun <Root, Type> KProperty1<Root, Map<String, Type>>.get(index: String): Field<Root, Type>

Refers to a specific item in a map, by its name.

gt

open infix fun <V> KProperty1<T, V>.gt(value: V)

Selects documents for which this field has a value strictly greater than value.

open infix fun <V> Field<T, V>.gt(value: V)

Selects documents for which this field has a value strictly greater than value.

gte

open infix fun <V> KProperty1<T, V>.gte(value: V)

Selects documents for which this field has a value greater or equal to value.

open infix fun <V> Field<T, V>.gte(value: V)

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.

open infix fun <V> Field<T, V>.gteNotNull(value: V?)

Selects documents for which this field has a value greater or equal to value.

gtNotNull

open infix fun <V> KProperty1<T, V>.gtNotNull(value: V?)

Selects documents for which this field has a value strictly greater than value.

open infix fun <V> Field<T, V>.gtNotNull(value: V?)

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.

open infix fun Field<T, *>.hasType(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.

abstract operator fun <V> Field<T, V>.invoke(block: FilterQueryPredicate<V>.() -> Unit)

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.

open fun Field<T, *>.isNotNull()

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.

open fun <V> Field<T, V>.isNotOneOf(vararg values: V)

Selects documents for which this field is not equal to any of the given values.

open fun <V> Field<T, V>.isNotOneOf(values: List<V>)

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.

open fun Field<T, *>.isNull()

Selects documents for which the field is null.

isOneOf

open fun <V> KProperty1<T, V>.isOneOf(vararg values: V)

Selects documents for which this field is equal to one of the given values.

open fun <V> KProperty1<T, V>.isOneOf(values: List<V>)

Selects documents for which this field is equal to one of the given values.

open fun <V> Field<T, V>.isOneOf(vararg values: V)

Selects documents for which this field is equal to one of the given values.

open fun <V> Field<T, V>.isOneOf(values: List<V>)

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

open infix fun <V> KProperty1<T, V>.lt(value: V)

Selects documents for which this field has a value strictly lesser than value.

open infix fun <V> Field<T, V>.lt(value: V)

Selects documents for which this field has a value strictly lesser than value.

lte

open infix fun <V> KProperty1<T, V>.lte(value: V)

Selects documents for which this field has a value lesser or equal to value.

open infix fun <V> Field<T, V>.lte(value: V)

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.

open infix fun <V> Field<T, V>.lteNotNull(value: V?)

Selects documents for which this field has a value lesser or equal to value.

ltNotNull

open infix fun <V> KProperty1<T, V>.ltNotNull(value: V?)

Selects documents for which this field has a value strictly lesser than value.

open infix fun <V> Field<T, V>.ltNotNull(value: V?)

Selects documents for which this field has a value strictly lesser than value.

ne

open infix fun <V> KProperty1<T, V>.ne(value: V)

Matches documents where the value of a field does not equal the value.

open infix fun <V> Field<T, V>.ne(value: V)

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.

open infix fun <V> Field<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.

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

open fun KProperty1<T, String?>.regex(pattern: String, caseInsensitive: Boolean = false, dotAll: Boolean = false, extended: Boolean = false, matchEachLine: Boolean = false)

Matches documents where the field corresponds to a given regex expression.

open fun Field<T, String?>.regex(pattern: String, caseInsensitive: Boolean = false, dotAll: Boolean = false, extended: Boolean = false, matchEachLine: Boolean = false)

Matches documents where the field corresponds to a given regex expression.

simplify

abstract fun simplify(): BsonNode?

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

abstract override fun toString(): String

JSON representation of this expression.

writeTo

abstract fun writeTo(writer: )

Writes the result of simplifying this expression into writer.