Skip to content

SchemaHostArrayQuery

Operators available in itemsInOrder.

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.

additionalItems

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

Specifies the behavior of itemsInOrder when more items are found than declared in the schema.

If this function is not called, additional items beyond the declared schemas are forbidden.

If this function is called, additional items beyond the declared schemas are allowed if they confirm to the schema.

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> {
                    // The first item must be a Double
                    schema("Latitude") {
                        type(BsonType.Double)
                    }

                    // The second item must be a Double
                    schema("Longitude") {
                        type(BsonType.Double)
                    }

                    // If there are other items, they must also be a Double
                    additionalItems("Altitude") {
                        type(BsonType.Double)
                    }
                }
            }
        }
    }
}

External resources

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.