Skip to content

MongoDB request DSLopensavvy.ktmongo.dsl.commandBulkWrite

BulkWrite

Performing multiple write operations in a single request.

Example

users.bulkWrite {
    updateOne({ User::name eq "foo" }) {
        User::age set 18
    }

    upsertOne({ User::name eq "bob" }) {
        User::age setOnInsert 18
        User::age inc 1
    }
}

Filtered writes

If we have multiple writes that share a similar filter, we can extract it to be common between them.

users.bulkWrite {
    updateOne({ User::name eq "foo" }) {
        User::age set 18
    }

    filtered({ User::isAlive eq true }) {
        updateMany({ User::name eq "bar" }) {
            User::age inc 2
        }

        updateOne({ User::name eq "baz" }) {
            User::age inc 1
        }
    }
}

To learn more, see filtered.

External resources

See also

Constructors

BulkWrite

constructor(context: BsonContext, globalFilter: FilterQuery<Document>.() -> Unit)

Properties

context

open val context: BsonContext

The context used to generate this expression.

operations

val operations: ERROR CLASS: Symbol not found for Sequence>

options

Functions

accept

open override fun accept(node: AvailableInBulkWrite<Document>)

Adds a new Node into the current node.

acceptAll

fun <N : Node> CompoundNode<N>.acceptAll(nodes: Iterable<N>)

Adds any number of nodes into this one.

filtered

fun filtered(filter: FilterQuery<Document>.() -> Unit, operations: BulkWrite<Document>.() -> Unit)

Declares a filter that is shared between all children operations.

freeze

open override fun freeze()

Makes this node immutable.

insertMany

fun insertMany(vararg documents: Document, options: InsertOneOptions<Document>.() -> Unit = {})

Inserts multiple documents in a single operation.

fun insertMany(documents: Iterable<Document>, options: InsertOneOptions<Document>.() -> Unit = {})

Inserts multiple documents in a single operation.

insertOne

fun insertOne(document: Document, options: InsertOneOptions<Document>.() -> Unit = {})

Inserts a document.

replaceOne

fun replaceOne(options: ReplaceOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit = {}, document: Document)

Replaces a document that matches filter by document.

repsertOne

fun repsertOne(options: ReplaceOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit = {}, document: Document)

Replaces a document that matches filter by document.

simplify

open fun simplify(): AbstractBsonNode?

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

toBson

open fun toBson(): ERROR CLASS: Symbol not found for Bson

Writes the result of simplifying to a new Bson.

toString

fun toString(simplified: Boolean): String

JSON representation of this expression.

override fun toString(): String

JSON representation of this expression.

updateMany

fun updateMany(options: UpdateOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit = {}, update: UpdateQuery<Document>.() -> Unit)

Updates all documents that match filter according to update.

updateOne

fun updateOne(options: UpdateOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit = {}, update: UpdateQuery<Document>.() -> Unit)

Updates a single document that matches filter according to update.

upsertOne

fun upsertOne(options: UpdateOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit = {}, update: UpsertQuery<Document>.() -> Unit)

Updates a single document that matches filter according to update.

writeTo

override fun writeTo(writer: ERROR CLASS: Symbol not found for BsonFieldWriter)

Writes the result of simplifying this expression into writer.