Skip to content

UpdatePipeline

An update pipeline.

Update pipelines allow more complex updates directly through the various update functions.

Example

users.updateManyWithPipeline {
    set {
        User::score set (of(User::score) + (of(User::scoreMultiplier) * of(User::dailyScore)))
        User::dailyScore set 0
    }
}

External resources

Properties

context

@LowLevelApi



abstract val context: BsonContext

The context used to generate this pipeline.

Can be accessed within children expressions.

Functions

project

Specifies a list of fields which should be kept in the document.

reinterpret

@DangerousMongoApi



@LowLevelApi



abstract fun <New : Any> reinterpret(): Pipeline<New>

Changes the type of the returned document, with no type-safety.

End-users should not need to call this function. This function is provided to allow stages to change the return document. No type verifications are made, it is solely the responsibility of the caller to ensure that the declared return type corresponds to the reality.

See also

set

open fun set(block: SetStageOperators<Document>.() -> Unit): Pipeline<Document>

Adds new fields to documents, or overwrites existing fields.

toString

abstract override fun toString(): String

JSON representation of this pipeline.

unset

Removes fields from documents.

withStage

@DangerousMongoApi



@LowLevelApi



abstract fun withStage(stage: BsonNode): Pipeline<Output>

Creates a new pipeline that expands on the current one by adding stage.

This method is analogous to CompoundBsonNode.accept, with the main difference that the latter mutates the current expression, whereas this method returns a new pipeline on which the stage is applied (because pipelines are immutable).

End-users should not need to call this function. All implemented stages provide an extension function on the Pipeline type. This function is provided for cases in which you need a stage that is not yet provided by the library. If that is your situation, start by reading AbstractBsonNode and AbstractCompoundBsonNode. If you want to proceed and implement your own stage, consider getting in touch with the maintainers of the library so it can be shared to all users.

The provided stage must validate the entire contract of BsonNode. Additionally, it should always emit the name of the stage first. For example, this is a valid stage:

"$match": {
    "name": "Bob"
}

but this isn't:

"name": "Bob"

because it doesn't start with a stage name.

Similarly, this isn't a valid stage, because it declares two different stage names:

"$match": {
    "name": "Bob"
},
"$set": {
    "foo": "bar"
}

See also

writeTo

@LowLevelApi



abstract fun writeTo(writer: BsonValueWriter)

Writes the entire pipeline into writer.

This function is similar to BsonNode.writeTo, with the difference that expressions generate documents, and pipelines generate arrays.

Using this method will thus write an array containing the different stages.