Skip to content

SchemaPropertyQuery

Operators available within SchemaQuery.SchemaPropertiesQuery.property.

Properties

context

The context used to generate this expression.

field

Converts a Kotlin property into a Field.

Number

A value of one of the numeric types: BsonType.Int32, BsonType.Int64, BsonType.Double or BsonType.Decimal128.

Example

class User(
    val _id: ObjectId,
    val age: Int,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::age) {
                type(BsonType.Number)
            }
        }
    }
}

See also

  • type: Specify a constraint on a value's type.

Functions

accept

@LowLevelApi
abstract fun accept(constraint: Schema.Constraint<T>)

Adds a new constraint to this schema.

We recommend against calling this function directly. Instead, you should find other functions declared on this object (possibly as extensions) that perform the operation you want safely.

acceptAll

Adds any number of nodes into this one.

allOf

abstract fun allOf(block: SchemaQuery.SchemaHostQuery<T>.() -> Unit)

Matches documents that satisfy all of the specified schemas.

Example

class User(
    val _id: ObjectId,
    val age: Int,
    val name: String,
)

users.find {
    schema("User") {
        allOf {
            schema("HasAge") {
                properties {
                    property(User::age) {
                        minimum(18)
                    }
                }
            }
            schema("HasName") {
                properties {
                    property(User::name) {
                        minLength(1)
                    }
                }
            }
        }
    }
}

External resources

anyOf

abstract fun anyOf(block: SchemaQuery.SchemaHostQuery<T>.() -> Unit)

Matches documents that satisfy at least one of the specified schemas.

Example

class User(
    val _id: ObjectId,
    val age: Int,
    val name: String,
)

users.find {
    schema("User") {
        anyOf {
            schema("Young") {
                properties {
                    property(User::age) {
                        maximum(17)
                    }
                }
            }
            schema("Named") {
                properties {
                    property(User::name) {
                        minLength(1)
                    }
                }
            }
        }
    }
}

External resources

dependsOn

Declares that if this property is present in the document, then the given property must also be present.

Example

class User(
    val _id: ObjectId,
    val shippingAddress: Address?,
    val billingAddress: Address?,
)

users.find {
    schema("User") {
        properties {
            val billing = property(User::billingAddress, required = false) {
                type(BsonType.Document)
            }
            property(User::shippingAddress, required = false) {
                type(BsonType.Document)
                dependsOn(billing)
            }
        }
    }
}

External resources

abstract fun dependsOn(property: String)

Declares that if this property is present in the document, then the property named property must also be present.

Example

class User(
    val _id: ObjectId,
    val shippingAddress: Address?,
    val billingAddress: Address?,
)

users.find {
    schema("User") {
        properties {
            property(User::billingAddress, required = false) {
                type(BsonType.Document)
            }
            property(User::shippingAddress, required = false) {
                type(BsonType.Document)
                dependsOn("billingAddress")
            }
        }
    }
}

External resources

abstract fun dependsOn(schema: SchemaQuery<R>.() -> Unit)

Declares that if this property is present in the document, then the document must also satisfy the schema given by schema.

Example

class User(
    val _id: ObjectId,
    val creditCard: String?,
    val billingAddress: Address?,
)

users.find {
    schema("User") {
        properties {
            property(User::creditCard, required = false) {
                type(BsonType.String)
                dependsOn {
                    properties {
                        property(User::billingAddress) {
                            type(BsonType.Document)
                        }
                    }
                }
            }
        }
    }
}

External resources

description

abstract fun description(description: String)

Provides a human-readable description of this schema.

This operator is purely documentation, it does not impact the query.

Example

class User(
    val _id: ObjectId,
    val age: Int,
)

users.find {
    schema("User") {
        description("Information about users")

        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::age) {
                description("The age of the user")
                type(BsonType.Number)
            }
        }
    }
}

External resources

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 <Root, Type, Child> Field<Root, Type>.div(child: KProperty1<in Type & Any, Child>): Field<Root, Child>

Refers to child as a nested field of the current field.

open operator fun <Root, Type, Child> Field<Root, Type>.div(child: Field<Type, Child>): Field<Root, Child>

Refers to child as a nested field of the current field.

enum

abstract fun enum(values: List<T>, type: KType)

inline fun <E : T> enum(vararg values: E)

Verifies that the value is one of the specified values.

Example

class User(
    val _id: ObjectId,
    val type: String,
)

enum class UserType {
    Admin,
    Member,
    Guest,
}

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::type) {
                enum(Admin, Member, Guest)
            }
        }
    }
}

External resources

freeze

@LowLevelApi
abstract override fun freeze()

Makes this node 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.

@JvmName(name = "bsonItem")
operator fun <Root, Child> KProperty1<Root, BsonArray>.get(index: Int): Field<Root, Child>

Refers to a child item of a field of type BsonArray.

@JvmName(name = "bsonField")
operator fun <Root, Child> KProperty1<Root, BsonDocument>.get(field: String): Field<Root, Child>

Refers to a child field of a field of type BsonDocument.

@JvmName(name = "bsonField")
operator fun <Child> BsonDocument.Companion.get(field: String): Field<BsonDocument, Child>

Refers to a root field of the document being analyzed.

open operator fun <Root, Type> Field<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> Field<Root, Map<String, Type>>.get(key: String): Field<Root, Type>

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

@JvmName(name = "bsonItem")
operator fun <Root, Child> Field<Root, BsonArray>.get(index: Int): Field<Root, Child>

Refers to a child item of a field of type BsonArray.

@JvmName(name = "bsonField")
operator fun <Root, Child> Field<Root, BsonDocument>.get(field: String): Field<Root, Child>

Refers to a child field of a field of type BsonDocument.

items

abstract fun <I> items(title: String, schema: SchemaQuery<I>.() -> Unit)

Restricts this property's array items to conform to schema.

Using this method, all items of the array must confirm to the specified schema. To specify different schema for the different items, see itemsInOrder.

If the property does not contain a BsonType.Array, this constraint is ignored.

Example

class User(
    val _id: ObjectId,
    val tags: List<String>,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::tags) {
                type(BsonType.Array)
                items<String>("Tag") {
                    type(BsonType.String)
                    maxLength(20)
                }
            }
        }
    }
}

External resources

itemsInOrder

abstract fun <I> itemsInOrder(schemas: SchemaQuery.SchemaHostArrayQuery<I>.() -> Unit)

Restricts this property's array items to conform to the given sequence of schemas.

The first item in the array must match the first declared schema, the second item must match the second declared schema, etc.

By default, if the array contains more items than declared schemas, validation fails. To allow additional items, declare an additionalItems schema inside schemas.

If the property does not contain a BsonType.Array, this constraint is ignored.

Example

class Coordinates(
    val _id: ObjectId,
    val values: List<Double>,
)

coordinates.find {
    schema("Coordinates") {
        properties {
            property(Coordinates::_id) {
                type(BsonType.ObjectId)
            }

            property(Coordinates::values) {
                type(BsonType.Array)
                itemsInOrder<Double> {
                    schema("Latitude") {
                        type(BsonType.Double)
                    }
                    schema("Longitude") {
                        type(BsonType.Double)
                    }
                }
            }
        }
    }
}

External resources

length

open fun length(length: Int)

Restricts this property's value to have exactly length characters.

If the property does not contain a BsonType.String, this constraint is ignored.

length must be non-negative.

This function is syntax sugar to set both minLength and maxLength.

Example

class User(
    val _id: ObjectId,
    val postalCode: String,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::postalCode) {
                type(BsonType.String)
                length(5)
            }
        }
    }
}
open fun length(length: IntRange)

open fun length(length: UIntRange)

Restricts this property's value to have a length compatible with length.

If the property does not contain a BsonType.String, this constraint is ignored.

This function is syntax sugar to set both minLength and maxLength.

Example

class User(
    val _id: ObjectId,
    val name: String,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::name) {
                type(BsonType.String)
                length(2..50)
            }
        }
    }
}

maximum

abstract fun maximum(number: Int, exclusive: Boolean = false)

abstract fun maximum(number: Long, exclusive: Boolean = false)

abstract fun maximum(number: Double, exclusive: Boolean = false)

Restricts this property's value to be less than number.

If the property does not contain a Number, this constraint is ignored.

Example

class User(
    val _id: ObjectId,
    val age: Int,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::age) {
                type(BsonType.Int32)
                maximum(120)
            }
        }
    }
}

External resources

maxItems

abstract fun maxItems(size: Int)

Restricts this property's array value to have at most size items (inclusive).

If the property does not contain a BsonType.Array, this constraint is ignored.

size must be non-negative.

Example

class User(
    val _id: ObjectId,
    val tags: List<String>,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::tags) {
                type(BsonType.Array)
                maxItems(5)
            }
        }
    }
}

External resources

maxLength

abstract fun maxLength(length: Int)

Restricts this property's value to have at most length characters (inclusive).

If the property does not contain a BsonType.String, this constraint is ignored.

length must be non-negative.

Example

class User(
    val _id: ObjectId,
    val name: String,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::name) {
                type(BsonType.String)
                maxLength(50)
            }
        }
    }
}

External resources

maxProperties

abstract fun maxProperties(count: Int)

Restricts this property's object value to have at most count properties (inclusive).

If the property does not contain a BsonType.Document, this constraint is ignored.

count must be non-negative.

Example

class User(
    val _id: ObjectId,
    val extraInfo: Map<String, String>,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::extraInfo) {
                type(BsonType.Document)
                maxProperties(5)
            }
        }
    }
}

External resources

minimum

abstract fun minimum(number: Int, exclusive: Boolean = false)

abstract fun minimum(number: Long, exclusive: Boolean = false)

abstract fun minimum(number: Double, exclusive: Boolean = false)

Restricts this property's value to be greater than number.

If the property does not contain a Number, this constraint is ignored.

Example

class User(
    val _id: ObjectId,
    val age: Int,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::age) {
                type(BsonType.Int32)
                minimum(0)
            }
        }
    }
}

External resources

minItems

abstract fun minItems(size: Int)

Restricts this property's array value to have at least size items (inclusive).

If the property does not contain a BsonType.Array, this constraint is ignored.

size must be non-negative.

Example

class User(
    val _id: ObjectId,
    val tags: List<String>,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::tags) {
                type(BsonType.Array)
                minItems(1)
            }
        }
    }
}

External resources

minLength

abstract fun minLength(length: Int)

Restricts this property's value to have at least length characters (inclusive).

If the property does not contain a BsonType.String, this constraint is ignored.

length must be non-negative.

Example

class User(
    val _id: ObjectId,
    val name: String,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::name) {
                type(BsonType.String)
                minLength(2)
            }
        }
    }
}

External resources

minProperties

abstract fun minProperties(count: Int)

Restricts this property's object value to have at least count properties (inclusive).

If the property does not contain a BsonType.Document, this constraint is ignored.

count must be non-negative.

Example

class User(
    val _id: ObjectId,
    val extraInfo: Map<String, String>,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::extraInfo) {
                type(BsonType.Document)
                minProperties(1)
            }
        }
    }
}

External resources

multipleOf

abstract fun multipleOf(number: Int)

abstract fun multipleOf(number: Long)

abstract fun multipleOf(number: Double)

Restricts this property's value to multiples of number.

If the property does not contain a Number, this constraint is ignored.

number must be strictly positive.

Example

class Product(
    val _id: ObjectId,
    val quantityInPack: Int,
)

products.find {
    schema("Product") {
        properties {
            property(Product::_id) {
                type(BsonType.ObjectId)
            }

            property(Product::quantityInPack) {
                type(BsonType.Int32)
                multipleOf(6)
            }
        }
    }
}

External resources

not

abstract fun not(title: String, block: SchemaQuery<T>.() -> Unit)

Matches documents that do not satisfy the specified schema.

Example

class User(
    val _id: ObjectId,
    val age: Int,
)

users.find {
    schema("User") {
        not("NotYoung") {
            properties {
                property(User::age) {
                    maximum(18)
                }
            }
        }
    }
}

External resources

oneOf

abstract fun oneOf(block: SchemaQuery.SchemaHostQuery<T>.() -> Unit)

Matches documents that satisfy exactly one of the specified schemas.

Example

class User(
    val _id: ObjectId,
    val age: Int,
    val name: String,
)

users.find {
    schema("User") {
        oneOf {
            schema("Young") {
                properties {
                    property(User::age) {
                        maximum(17)
                    }
                }
            }
            schema("Adult") {
                properties {
                    property(User::age) {
                        minimum(18)
                    }
                }
            }
        }
    }
}

External resources

pattern

abstract fun pattern(@Language(value = "JSRegexp") pattern: String)

Restricts this property's value to match the regular expression pattern, in the ECMA 262 dialect.

If the property does not contain a String, this constraint is ignored.

Example

class User(
    val _id: ObjectId,
    val email: String,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::email) {
                type(BsonType.String)
                pattern("^.+@.+\\..+$")
            }
        }
    }
}

External resources

properties

open fun properties(count: Int)

Restricts this property's object value to have exactly count properties.

If the property does not contain a BsonType.Document, this constraint is ignored.

count must be non-negative.

This function is syntax sugar to set both minProperties and maxProperties.

Example

class User(
    val _id: ObjectId,
    val extraInfo: Map<String, String>,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::extraInfo) {
                type(BsonType.Document)
                properties(5)
            }
        }
    }
}

External resources

open fun properties(count: IntRange)

Restricts this property's object value to have between count.first and count.last properties (inclusive).

If the property does not contain a BsonType.Document, this constraint is ignored.

count must be non-negative.

This function is syntax sugar to set both minProperties and maxProperties.

Example

class User(
    val _id: ObjectId,
    val extraInfo: Map<String, String>,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::extraInfo) {
                type(BsonType.Document)
                properties(1..5)
            }
        }
    }
}

External resources

open fun properties(count: UIntRange)

Restricts this property's object value to have between count.first and count.last properties (inclusive).

If the property does not contain a BsonType.Document, this constraint is ignored.

This function is syntax sugar to set both minProperties and maxProperties.

Example

class User(
    val _id: ObjectId,
    val extraInfo: Map<String, String>,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::extraInfo) {
                type(BsonType.Document)
                properties(1u..5u)
            }
        }
    }
}

External resources

abstract fun properties(block: SchemaQuery.SchemaPropertiesQuery<T>.() -> Unit)

Declares constraints on the properties of this object.

If the property does not contain a BsonType.Document, this constraint is ignored.

By default, the document is allowed to have additional properties that match none of the configured field. To control properties that are not declared, see SchemaPropertiesQuery.additionalProperties.

Example

class User(
    val _id: ObjectId,
    val name: String,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }
            property(User::name) {
                type(BsonType.String)
            }
        }
    }
}

External resources

simplify

@LowLevelApi
abstract fun simplify(): BsonNode?

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

size

open fun size(size: Int)

Restricts this property's array value to have exactly size items.

If the property does not contain a BsonType.Array, this constraint is ignored.

size must be non-negative.

This function is syntax sugar to set both minItems and maxItems.

Example

class User(
    val _id: ObjectId,
    val tags: List<String>,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::tags) {
                type(BsonType.Array)
                size(5)
            }
        }
    }
}

External resources

open fun size(size: IntRange)

Restricts this property's array value to have between size.first and size.last items (inclusive).

If the property does not contain a BsonType.Array, this constraint is ignored.

The range boundaries must be non-negative.

This function is syntax sugar to set both minItems and maxItems.

Example

class User(
    val _id: ObjectId,
    val tags: List<String>,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::tags) {
                type(BsonType.Array)
                size(1..5)
            }
        }
    }
}

External resources

open fun size(size: UIntRange)

Restricts this property's array value to have between size.first and size.last items (inclusive).

If the property does not contain a BsonType.Array, this constraint is ignored.

This function is syntax sugar to set both minItems and maxItems.

Example

class User(
    val _id: ObjectId,
    val tags: List<String>,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::tags) {
                type(BsonType.Array)
                size(1..5)
            }
        }
    }
}

External resources

toBson

Writes the result of simplifying to a new BsonDocument.

toString

abstract override fun toString(): String

JSON representation of this expression.

type

abstract fun type(bsonType: BsonType)

Verifies that the value has type bsonType.

Example

class User(
    val _id: ObjectId,
    val age: Int,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::age) {
                type(BsonType.Int32)
            }
        }
    }
}

External resources

abstract fun type(bsonType: SchemaQuery.Number)

Verifies that the value has one of the types BsonType.Int32, BsonType.Int64, BsonType.Double or BsonType.Decimal128.

Example

class User(
    val _id: ObjectId,
    val age: Int,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::age) {
                type(BsonType.Number)
            }
        }
    }
}

External resources

uniqueItems

abstract fun uniqueItems(unique: Boolean = true)

Restricts this property's array value to only contain unique items.

If the property does not contain a BsonType.Array, this constraint is ignored.

If unique is false, duplicate items are allowed.

Example

class User(
    val _id: ObjectId,
    val tags: List<String>,
)

users.find {
    schema("User") {
        properties {
            property(User::_id) {
                type(BsonType.ObjectId)
            }

            property(User::tags) {
                type(BsonType.Array)
                uniqueItems()
            }
        }
    }
}

External resources

unsafe

open infix fun <Root, Child> KProperty1<Root, *>.unsafe(child: String): Field<Root, Child>

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.

open infix fun <Root, Child> KProperty1<Root, *>.unsafe(child: Field<*, 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.

open infix fun <Root, Child> Field<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.

open infix fun <Root, Child> Field<Root, *>.unsafe(child: Field<*, 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.

open infix fun <Root, Type, Child> Field<Root, Type>.unsafe(child: String): Field<Root, Child>

Refers to a field child of the current field, with no compile-time safety.

unsafeCast

Overwrites the represented type of this field.

writeTo

@LowLevelApi
abstract override fun writeTo(writer: BsonFieldWriter)

Writes the result of simplifying this expression into writer.