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: , globalFilter: FilterQuery<Document>.() -> Unit)

Properties

context

abstract val context: 

The context used to generate this expression.

operations

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

abstract override fun freeze()

Makes this expression 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.

simplify

abstract fun simplify(): BsonNode?

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

toBson

open fun toBson(): 

Writes the result of simplifying to a new Bson.

toString

fun toString(simplified: Boolean): String

JSON representation of this expression.

abstract 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

abstract fun writeTo(writer: )

Writes the result of simplifying this expression into writer.