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

Link copied to clipboard
open val <V> KProperty1<T, Collection<V>>.any: Field<T, V>
open val <V> Field<T, Collection<V>>.any: Field<T, V>

Specify operators on array elements.

Link copied to clipboard
abstract val context: <Error class: unknown class>

The context used to generate this expression.

Link copied to clipboard

Converts a Kotlin property into a Field.

Functions

Link copied to clipboard
abstract override fun accept(node: BsonNode)

Adds a new node as a child of this one.

Link copied to clipboard
fun <N : Node> CompoundNode<N>.acceptAll(nodes: Iterable<N>)

Adds any number of nodes into this one.

Link copied to clipboard
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.

Link copied to clipboard
open fun <V> KProperty1<T, Collection<V>>.any(block: FilterQuery<V>.() -> Unit)
abstract fun <V> Field<T, Collection<V>>.any(block: FilterQuery<V>.() -> Unit)

Specify multiple operators on fields of a single array element.

Link copied to clipboard
abstract fun <V> Field<T, Collection<V>>.anyValue(block: FilterQueryPredicate<V>.() -> Unit)

Specify multiple operators on a single array element.

Link copied to clipboard
open infix fun KProperty1<T, *>.bitsAllClear(mask: <Error class: unknown class>)
open infix fun KProperty1<T, *>.bitsAllClear(mask: ByteArray)
open infix fun Field<T, *>.bitsAllClear(mask: <Error class: unknown class>)
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.

Link copied to clipboard
open infix fun KProperty1<T, *>.bitsAllSet(mask: <Error class: unknown class>)
open infix fun KProperty1<T, *>.bitsAllSet(mask: ByteArray)
open infix fun Field<T, *>.bitsAllSet(mask: <Error class: unknown class>)
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.

Link copied to clipboard
open infix fun KProperty1<T, *>.bitsAnyClear(mask: <Error class: unknown class>)
open infix fun KProperty1<T, *>.bitsAnyClear(mask: ByteArray)
open infix fun Field<T, *>.bitsAnyClear(mask: <Error class: unknown class>)
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.

Link copied to clipboard
open infix fun KProperty1<T, *>.bitsAnySet(mask: <Error class: unknown class>)
open infix fun KProperty1<T, *>.bitsAnySet(mask: ByteArray)
open infix fun Field<T, *>.bitsAnySet(mask: <Error class: unknown class>)
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.

Link copied to clipboard
open infix fun <V> KProperty1<T, Collection<V>>.containsAll(values: Collection<V>)
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.

Link copied to clipboard
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>
open operator fun <V, V2> KProperty1<T, Collection<V>>.div(other: Field<V, V2>): Field<T, V2>
open operator fun <V, V2> Field<T, Collection<V>>.div(other: KProperty1<V, V2>): Field<T, V2>
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.

Link copied to clipboard
open fun KProperty1<T, *>.doesNotExist()
open fun Field<T, *>.doesNotExist()

Matches documents that do not contain the specified field. Documents where the field if null are not matched.

Link copied to clipboard
open infix fun <V> KProperty1<T, V>.eq(value: V)
open infix fun <V> Field<T, V>.eq(value: V)

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

Link copied to clipboard
open infix fun <V> KProperty1<T, V>.eqNotNull(value: V?)
open infix fun <V> Field<T, V>.eqNotNull(value: V?)

Matches documents where the value of a field equals value.

Link copied to clipboard
open fun KProperty1<T, *>.exists()
open fun Field<T, *>.exists()

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

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

Enables the usage of aggregation values within a regular query.

Link copied to clipboard
abstract override fun freeze()

Makes this expression immutable.

Link copied to clipboard
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.

Link copied to clipboard
open infix fun <V> KProperty1<T, V>.gt(value: V)
open infix fun <V> Field<T, V>.gt(value: V)

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

Link copied to clipboard
open infix fun <V> KProperty1<T, V>.gte(value: V)
open infix fun <V> Field<T, V>.gte(value: V)

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

Link copied to clipboard
open infix fun <V> KProperty1<T, V>.gteNotNull(value: V?)
open infix fun <V> Field<T, V>.gteNotNull(value: V?)

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

Link copied to clipboard
open infix fun <V> KProperty1<T, V>.gtNotNull(value: V?)
open infix fun <V> Field<T, V>.gtNotNull(value: V?)

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

Link copied to clipboard
open infix fun KProperty1<T, *>.hasType(type: <Error class: unknown class>)
open infix fun Field<T, *>.hasType(type: <Error class: unknown class>)

Selects documents where the value of the field is an instance of the specified BSON type.

Link copied to clipboard
open operator fun <V> KProperty1<T, V>.invoke(block: FilterQueryPredicate<V>.() -> Unit)
abstract operator fun <V> Field<T, V>.invoke(block: FilterQueryPredicate<V>.() -> Unit)

Targets a single field to execute a targeted predicate.

Link copied to clipboard
open fun KProperty1<T, Collection<*>>.isEmpty()
open fun Field<T, Collection<*>>.isEmpty()

Matches documents in which an array is empty or absent.

Link copied to clipboard
open infix fun <V : Comparable<V>, R : ClosedRange<V>, OpenEndRange<V>> KProperty1<T, V?>.isIn(range: R)
open infix fun <V : Comparable<V>> KProperty1<T, V?>.isIn(range: ClosedRange<V>)
open infix fun <V : Comparable<V>> KProperty1<T, V?>.isIn(range: OpenEndRange<V>)
open infix fun <V : Comparable<V>, R : ClosedRange<V>, OpenEndRange<V>> Field<T, V?>.isIn(range: R)
open infix fun <V : Comparable<V>> Field<T, V?>.isIn(range: ClosedRange<V>)
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.

Link copied to clipboard
open fun KProperty1<T, Map<String, *>>.isMapEmpty()
open fun Field<T, Map<String, *>>.isMapEmpty()

Matches documents in which a map is empty or absent.

Link copied to clipboard
open fun Field<T, Map<String, *>>.isMapNotEmpty()

Matches documents in which a map is not empty.

Link copied to clipboard
open fun Field<T, Collection<*>>.isNotEmpty()

Matches documents in which an array is not empty.

Link copied to clipboard
open fun KProperty1<T, *>.isNotNull()
open fun Field<T, *>.isNotNull()

Selects documents for which the field is not null.

Link copied to clipboard
open fun <V> KProperty1<T, V>.isNotOneOf(vararg values: V)
open fun <V> KProperty1<T, V>.isNotOneOf(values: List<V>)
open fun <V> Field<T, V>.isNotOneOf(vararg values: V)
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.

Link copied to clipboard
open fun KProperty1<T, *>.isNotUndefined()
open fun Field<T, *>.isNotUndefined()

Selects documents for which the field is not undefined.

Link copied to clipboard
open fun KProperty1<T, *>.isNull()
open fun Field<T, *>.isNull()

Selects documents for which the field is null.

Link copied to clipboard
open fun <V> KProperty1<T, V>.isOneOf(vararg values: V)
open fun <V> KProperty1<T, V>.isOneOf(values: List<V>)
open fun <V> Field<T, V>.isOneOf(vararg values: V)
open fun <V> Field<T, V>.isOneOf(values: List<V>)

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

Link copied to clipboard
open fun KProperty1<T, *>.isUndefined()
open fun Field<T, *>.isUndefined()

Selects documents for which the field is undefined.

Link copied to clipboard
open infix fun <V> KProperty1<T, V>.lt(value: V)
open infix fun <V> Field<T, V>.lt(value: V)

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

Link copied to clipboard
open infix fun <V> KProperty1<T, V>.lte(value: V)
open infix fun <V> Field<T, V>.lte(value: V)

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

Link copied to clipboard
open infix fun <V> KProperty1<T, V>.lteNotNull(value: V?)
open infix fun <V> Field<T, V>.lteNotNull(value: V?)

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

Link copied to clipboard
open infix fun <V> KProperty1<T, V>.ltNotNull(value: V?)
open infix fun <V> Field<T, V>.ltNotNull(value: V?)

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

Link copied to clipboard
open infix fun <V> KProperty1<T, V>.ne(value: V)
open infix fun <V> Field<T, V>.ne(value: V)

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

Link copied to clipboard
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.

Link copied to clipboard
open infix fun <V> KProperty1<T, V>.not(expression: FilterQueryPredicate<V>.() -> Unit)
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.

Link copied to clipboard
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.

Link copied to clipboard
open fun KProperty1<T, String?>.regex(pattern: String, caseInsensitive: Boolean = false, dotAll: Boolean = false, extended: Boolean = false, matchEachLine: Boolean = false)
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.

Link copied to clipboard
abstract fun simplify(): BsonNode?

Returns a simplified (but equivalent) expression to the current expression.

Link copied to clipboard
open infix fun KProperty1<T, Collection<*>>.size(size: Int)
abstract infix fun Field<T, Collection<*>>.size(size: Int)

Selects documents where the value of a field is an array of size size (exactly).

Link copied to clipboard
open fun toBson(): <Error class: unknown class>

Writes the result of simplifying to a new Bson.

Link copied to clipboard
abstract override fun toString(): String

JSON representation of this expression.

Link copied to clipboard
abstract fun writeTo(writer: <Error class: unknown class>)

Writes the result of simplifying this expression into writer.