bulkWrite
open suspend override fun bulkWrite(options: BulkWriteOptions<Document>.() -> Unit, filter: FilterQuery<Document>.() -> Unit, operations: BulkWrite<Document>.() -> Unit)(source)
Performs multiple update operations in a single request.
Example
class User(
val name: String,
val age: Int,
)
collection.bulkWrite {
upsertOne(
filter = {
User::name eq "Patrick"
},
update = {
User::age set 15
}
)
updateMany {
User::age inc 1
}
}
Content copied to clipboard
To see which operations are available and their respective syntax, see BulkWrite.
Using filtered writes
We can group operations by the filter they apply on:
collection.bulkWrite {
filtered(filter = { User::isAlive eq true }) {
updateOne(…)
updateOne(…)
updateMany(…)
}
updateOne(…)
}
Content copied to clipboard
To learn more, see filtered.
Using filtered collections
If we want all operations to use the same filter, we can declare it before calling the operation:
collection.filter {
User::isAlive eq true
}.bulkWrite {
updateOne(…)
updateOne(…)
updateMany(…)
}
Content copied to clipboard