Skip to content

SchemaHostQuery

DSL to allow declaring new schema. For more information, see SchemaQuery.

Inheritors

Properties

context

The context used to generate this expression.

Functions

accept

@LowLevelApi
@DangerousMongoApi
abstract override fun accept(node: BsonNode)

Adds a new node as a child of this one.

acceptAll

Adds any number of nodes into this one.

freeze

@LowLevelApi
abstract override fun freeze()

Makes this node immutable.

schema

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

Matches documents that satisfy the specified schema.

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)
            }
        }
    }
}

To see the list of operators, read SchemaQuery.

External resources

simplify

@LowLevelApi
abstract fun simplify(): BsonNode?

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

toBson

Writes the result of simplifying to a new BsonDocument.

toString

abstract override fun toString(): String

JSON representation of this expression.

writeTo

@LowLevelApi
abstract override fun writeTo(writer: BsonFieldWriter)

Writes the result of simplifying this expression into writer.