FilterQueryPredicate¶
interface FilterQueryPredicate<T> : CompoundBsonNode, FieldDsl
DSL for MongoDB operators that are used as predicates in conditions in a context where the targeted field is already specified.
Example¶
- By referring to a specific property, we obtain a
FilterQueryPredicatethat we can use to declare many operators on that property.
If you can't find the operator you're searching for, visit the tracking issue.
External resources¶
Parameters¶
- T: The type on which this predicate applies. For example, if the selected field is of type
String, thenTisString.
Constructors¶
FilterQueryPredicate¶
@LowLevelApi
fun <T> FilterQueryPredicate(context: BsonContext): FilterQueryPredicate<T>
Creates an empty FilterQueryPredicate.
Properties¶
context¶
@LowLevelApi
abstract val context: BsonContext
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.
bitsAllClear¶
abstract fun bitsAllClear(mask: UInt)
Matches documents where all bit positions present in mask are clear (i.e., 0) in the current field.
This operator will not match numerical values that cannot be represented as a signed 64-bit integer (e.g. Decimal128) nor ones that have a fractional component.
Example¶
class User(
val name: String,
val age: Int?,
)
collection.find {
User::age {
bitsAllClear(UInt.MAX_VALUE)
}
}
Performance¶
Queries cannot use indexes for this operator, but they can use indexes for other operators.
External resources¶
abstract fun bitsAllClear(mask: ByteArray)
Matches documents where all bit positions present in mask are clear (i.e., 0) in the current field.
This operator will not match numerical values that cannot be represented as a signed 64-bit integer (e.g. Decimal128) nor ones that have a fractional component.
Performance¶
Queries cannot use indexes for this operator, but they can use indexes for other operators.
External resources¶
bitsAllSet¶
abstract fun bitsAllSet(mask: UInt)
Matches documents where all bit positions present in mask are set (i.e., 1) in the current field.
This operator will not match numerical values that cannot be represented as a signed 64-bit integer (e.g. Decimal128) nor ones that have a fractional component.
Example¶
class User(
val name: String,
val age: Int?,
)
collection.find {
User::age {
bitsAllSet(UInt.MAX_VALUE)
}
}
Performance¶
Queries cannot use indexes for this operator, but they can use indexes for other operators.
External resources¶
abstract fun bitsAllSet(mask: ByteArray)
Matches documents where all bit positions present in mask are set (i.e., 1) in the current field.
This operator will not match numerical values that cannot be represented as a signed 64-bit integer (e.g. Decimal128) nor ones that have a fractional component.
Performance¶
Queries cannot use indexes for this operator, but they can use indexes for other operators.
External resources¶
bitsAnyClear¶
abstract fun bitsAnyClear(mask: UInt)
Matches documents where any bit position present in mask is clear (i.e., 0) in the current field.
This operator will not match numerical values that cannot be represented as a signed 64-bit integer (e.g. Decimal128) nor ones that have a fractional component.
Example¶
class User(
val name: String,
val age: Int?,
)
collection.find {
User::age {
bitsAnyClear(UInt.MAX_VALUE)
}
}
Performance¶
Queries cannot use indexes for this operator, but they can use indexes for other operators.
External resources¶
abstract fun bitsAnyClear(mask: ByteArray)
Matches documents where any bit position present in mask is clear (i.e., 0) in the current field.
This operator will not match numerical values that cannot be represented as a signed 64-bit integer (e.g. Decimal128) nor ones that have a fractional component.
Performance¶
Queries cannot use indexes for this operator, but they can use indexes for other operators.
External resources¶
bitsAnySet¶
abstract fun bitsAnySet(mask: UInt)
Matches documents where any bit position present in mask is set (i.e., 1) in the current field.
This operator will not match numerical values that cannot be represented as a signed 64-bit integer (e.g. Decimal128) nor ones that have a fractional component.
Example¶
class User(
val name: String,
val age: Int?,
)
collection.find {
User::age {
bitsAnySet(UInt.MAX_VALUE)
}
}
Performance¶
Queries cannot use indexes for this operator, but they can use indexes for other operators.
External resources¶
abstract fun bitsAnySet(mask: ByteArray)
Matches documents where any bit position present in mask is set (i.e., 1) in the current field.
This operator will not match numerical values that cannot be represented as a signed 64-bit integer (e.g. Decimal128) nor ones that have a fractional component.
Performance¶
Queries cannot use indexes for this operator, but they can use indexes for other operators.
External resources¶
div¶
Refers to child as a nested field of the current field.
Refers to child as a nested field of the current field.
Refers to child as a nested field of the current field.
doesNotExist¶
abstract fun doesNotExist()
Matches documents that do not contain the specified field. Documents where the field if null are counted as existing.
Example¶
External resources¶
See also
-
FilterQuery.doesNotExist: Shorthand. -
FilterQueryPredicate.exists: Opposite. -
FilterQueryPredicate.isNull: Only matches elements that are specificallynull.
eq¶
Matches documents where the value of a field equals the value.
Example¶
External resources¶
See also
FilterQuery.eq: Shorthand.
eqNotNull¶
Matches documents where the value of a field equals value.
If value is null, the operator is not added (all documents are matched).
Example¶
This operator is useful to simplify searches when the criteria is optional. For example, instead of writing:
this operator can be used instead:
External resources¶
See also
-
FilterQuery.eqNotNull: Shorthand. -
FilterQueryPredicate.eq: Equality filter.
exists¶
abstract fun exists()
Matches documents that contain the specified field, including values where the field value is null.
Example¶
External resources¶
See also
-
FilterQuery.exists: Shorthand. -
FilterQueryPredicate.doesNotExist: Opposite. -
FilterQueryPredicate.isNotNull: Identical, but does not match elements where the field isnull.
freeze¶
@LowLevelApi
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.
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.
Example¶
External resources¶
See also
gte¶
Selects documents for which this field has a value greater or equal to value.
Example¶
External resources¶
See also
gteNotNull¶
open fun gteNotNull(value: T?)
Selects documents for which this field has a value greater or equal to value.
If value is null, the operator is not added (all elements are matched).
Example¶
External resources¶
See also
FilterQuery.gteNotNullFilterQueryPredicate.eqNotNull: Learn more about the 'notNull' variants
gtNotNull¶
Selects documents for which this field has a value strictly greater than value.
If value is null, the operator is not added (all elements are matched).
Example¶
External resources¶
See also
FilterQuery.gtNotNullFilterQueryPredicate.eqNotNull: Learn more about the 'notNull' variants
hasType¶
Selects documents where the value of the field is an instance of the specified BSON type.
Querying by data type is useful when dealing with highly unstructured data where data types are not predictable.
Example¶
class User(
val name: String,
val age: Any,
)
collection.find {
User::age {
type(BsonType.STRING)
}
}
External resources¶
See also
-
FilterQuery.hasType: Shorthand. -
FilterQueryPredicate.isNull: Checks if a value has the typeBsonType.Null. -
FilterQueryPredicate.isUndefined: Checks if a value has the typeBsonType.Undefined.
isNotNull¶
open fun isNotNull()
Selects documents for which the field is not null.
Example¶
External resources¶
See also
-
FilterQuery.isNotNull: Shorthand. -
FilterQueryPredicate.isNull: Opposite.
isNotOneOf¶
abstract fun isNotOneOf(values: Collection<T>)
Selects documents for which this field is not equal to any of the given values.
This operator will also select documents for which the field doesn't exist.
Example¶
class User(
val name: String,
val age: Int?,
)
collection.find {
User::name {
isNotOneOf(listOf("Alfred", "Arthur"))
}
}
External resources¶
See also
- FilterExpression.isNotOneOf
FilterQueryPredicate.ne
open fun isNotOneOf(vararg values: T)
Selects documents for which this field is not equal to any of the given values.
This operator will also select documents for which the field doesn't exist.
Example¶
class User(
val name: String,
val age: Int?,
)
collection.find {
User::name {
isNotOneOf("Alfred", "Arthur")
}
}
External resources¶
See also
- FilterExpression.isNotOneOf
FilterQueryPredicate.ne
isNotUndefined¶
open fun isNotUndefined()
Deprecated¶
This functionality is deprecated in the BSON specification. See https://bsonspec.org/spec.html
Selects documents for which the field is not undefined.
Example¶
External resources¶
See also
-
FilterQuery.isNotUndefined: Shorthand. -
FilterQueryPredicate.isUndefined: Opposite.
isNull¶
open fun isNull()
Selects documents for which the field is null.
Example¶
External resources¶
See also
-
FilterQuery.isNull: Shorthand. -
FilterQueryPredicate.doesNotExist: Checks if the value is not set. -
FilterQueryPredicate.isNotNull: Opposite.
isOneOf¶
abstract fun isOneOf(values: Collection<T>)
Selects documents for which this field is equal to one of the given values.
Example¶
class User(
val name: String,
val age: Int?,
)
collection.find {
User::name {
isOneOf(listOf("Alfred", "Arthur"))
}
}
External resources¶
See also
Selects documents for which this field is equal to one of the given values.
Example¶
class User(
val name: String,
val age: Int?,
)
collection.find {
User::name {
isOneOf("Alfred", "Arthur")
}
}
External resources¶
See also
isUndefined¶
open fun isUndefined()
Deprecated¶
This functionality is deprecated in the BSON specification. See https://bsonspec.org/spec.html
Selects documents for which the field is undefined.
Example¶
External resources¶
See also
-
FilterQuery.isUndefined: Shorthand. -
FilterQueryPredicate.isNotUndefined: Opposite.
lt¶
Selects documents for which this field has a value strictly lesser than value.
Example¶
External resources¶
See also
lte¶
Selects documents for which this field has a value lesser or equal to value.
Example¶
External resources¶
See also
lteNotNull¶
open fun lteNotNull(value: T?)
Selects documents for which this field has a value lesser or equal to value.
If value is null, the operator is not added (all elements are matched).
Example¶
External resources¶
See also
FilterQuery.lteNotNullFilterQueryPredicate.eqNotNull: Learn more about the 'notNull' variants
ltNotNull¶
Selects documents for which this field has a value strictly lesser than value.
If value is null, the operator is not added (all elements are matched).
Example¶
External resources¶
See also
FilterQuery.ltNotNullFilterQueryPredicate.ltNotNull: Learn more about the 'notNull' variants
ne¶
Matches documents where the value of a field does not equal the value.
The result includes documents which do not contain the specified field.
Example¶
External resources¶
See also
FilterQuery.ne: Shorthand.
not¶
abstract fun not(expression: FilterQueryPredicate<T>.() -> 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.
Example¶
class User(
val name: String,
val age: Int,
)
collection.find {
User::age {
not {
hasType(BsonType.STRING)
}
}
}
External resources¶
See also
FilterQuery.not: Shorthand.
regex¶
Matches documents where the field corresponds to a given regex expression.
Example¶
Indexing¶
If possible, prefer using a "^" prefix. For example, if we know that a pattern will only be present at the start of a string, "^foo" will use indexes, whereas "foo" will not.
Avoid using .* at the start and end of a pattern. "foo" is identical to "foo.*", but the former can use indexes and the latter cannot.
External resources¶
Parameters
-
caseInsensitive: If
true, the result is matched even if its case doesn't match. Note that this also disables index usage (even case-insensitive indexes) and ignores collation. -
dotAll: If
true, the dot character (.) can match newlines. -
extended: If
true, whitespace (except in character classes) is ignored, and segments starting from an unescaped pound (#) until a newline are ignored, similarly to a Python comment.
User::name.regex(
pattern = """
abc # This is a comment, it's not part of the pattern
123
""".trimIndent(),
extended = true,
)
which is identical to the non-extended pattern "abc123".
- matchEachLine: If
true, the special characters^and$match the beginning and end of each line, instead of matching the beginning and end of the entire string. Therefore,"^S"will match"First line\nSecond line", which would not match otherwise.
See also
FilterQuery.regex: Shorthand syntax
simplify¶
Returns a simplified (but equivalent) expression to the current expression.
toBson¶
Writes the result of simplifying to a new BSON document.
toString¶
JSON representation of this expression.
unsafe¶
Refers to a field child of the current field, with no compile-time safety.
open infix fun <Root, Child> KProperty1<Root, *>.unsafe(child: KProperty1<*, Child>): Field<Root, Child>
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, with no compile-time safety.
writeTo¶
@LowLevelApi
abstract override fun writeTo(writer: BsonFieldWriter)
Writes the result of simplifying this expression into writer.