Skip to content

AbstractValue

@LowLevelApi
abstract class AbstractValue<Root : Any, Type> : Node, Value<Root, Type> 

Utility implementation of Value, which handles the context, toString representation and freezing.

Implementing a new operator

Implementing class is identical in concept to implementing AbstractBsonNode. The main difference is the writer is a BsonValueWriter instead of a BsonFieldWriter.

Constructors

AbstractValue

constructor(context: BsonContext)

Properties

context

@LowLevelApi
open override val context: BsonContext

The context used to generate this value.

Functions

freeze

@LowLevelApi
open override fun freeze()

Makes this node immutable.

simplify

@LowLevelApi
open override fun simplify(): AbstractValue<Root, Type>

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

toString

fun toString(simplified: Boolean): String

JSON representation of this expression.

By default, simplifications are enabled. Set simplified to false to disable simplifications.

override fun toString(): String

JSON representation of this expression.

Note that since this class represents a BSON value, and BSON libraries often only support documents, the actual value may be surrounded by some boilerplate (like an array or a useless value).

unsafeCast

open fun <New> unsafeCast(): Value<Root, New>

Overwrites the represented type of this value.

This method can be useful to bypass type checks.

Example

If a field has changed type over time, the DTO will use the newer type. However, you may still want to write a query using the old type:

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

users.filter { User::age hasType BsonType.Double }
    .updateManyWithPipeline {
        set {
            User::age set User::age.unsafeCast<Double>() // Reflect the old type
               .toInt() // Convert to the new type
        }
    }

unsafeNonNull

Ignores the case where this value is null.

The KtMongo library will trust that the value is non-null.

writeTo

@LowLevelApi
override fun writeTo(writer: BsonValueWriter)

Writes the result of simplifying this value into writer.