SchemaQuery¶
interface SchemaQuery<T> : CompoundBsonNode, FieldDsl
DSL for the `$jsonSchema operator`.
Example¶
Find all documents that can be successfully deserialized:
class City(
val _id: ObjectId,
val name: String,
val countryCode: String,
val popularity: Int? = null,
)
collection.find {
schema("City") {
type(BsonType.Document)
properties {
property(City::_id) {
type(BsonType.ObjectId)
}
property(City::name) {
type(BsonType.String)
}
property(City::countryCode) {
type(BsonType.String)
length(2)
}
property(City::popularity, required = false) {
type(BsonType.Int32)
}
}
}
}
Operators¶
Documentation:
General operators:
Numeric operators:
String operators:
Array operators:
Document operators:
-
SchemaPropertiesQuery.Property.dependsOn
Combination operators:
External resources¶
Inheritors¶
Constructors¶
SchemaQuery¶
@LowLevelApi
fun <T> SchemaQuery(title: String, context: BsonContext): SchemaQuery<T>
Types¶
Number¶
object Number
A value of one of the numeric types: BsonType.Int32, BsonType.Int64, BsonType.Double or BsonType.Decimal128.
SchemaHostArrayQuery¶
interface SchemaHostArrayQuery<T> : SchemaQuery.SchemaHostQuery<T>
Operators available in itemsInOrder.
SchemaHostQuery¶
interface SchemaHostQuery<T> : CompoundBsonNode
DSL to allow declaring new schema. For more information, see SchemaQuery.
SchemaPropertiesQuery¶
interface SchemaPropertiesQuery<T> : CompoundBsonNode, FieldDsl
Properties¶
context¶
@LowLevelApi
abstract val context: BsonContext
The context used to generate this expression.
field¶
Converts a Kotlin property into a Field.
Number¶
open val BsonType.Companion.Number: SchemaQuery.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¶
@LowLevelApi
@DangerousMongoApi
fun <N : Node> CompoundNode<N>.acceptAll(nodes: Iterable<N>)
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
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¶
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.
enum¶
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¶
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 child item of a field of type BsonArray.
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.
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 child item of a field of type BsonArray.
Refers to a child field of a field of type BsonDocument.
items¶
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¶
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
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
maximum¶
Restricts this property's value to be less than number.
-
If
exclusiveistrue, the value must be strictly less thannumber. -
If
exclusiveisfalse, the value must be less than or equal tonumber.
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¶
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¶
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¶
Restricts this property's value to be greater than number.
-
If
exclusiveistrue, the value must be strictly greater thannumber. -
If
exclusiveisfalse, the value must be greater than or equal tonumber.
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¶
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¶
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)
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¶
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¶
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
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
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¶
@LowLevelApi
open fun toBson(): BsonDocument
Writes the result of simplifying to a new BsonDocument.
toString¶
type¶
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¶
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.
unsafeCast¶
open fun <Root, NewType> KProperty1<Root, *>.unsafeCast(): Field<Root, NewType>
Overwrites the represented type of this field.
writeTo¶
@LowLevelApi
abstract override fun writeTo(writer: BsonFieldWriter)
Writes the result of simplifying this expression into writer.