BulkWrite¶
class BulkWrite<Document : Any> : Command, AbstractBsonNode, CompoundNode<AvailableInBulkWrite<Document>>
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¶
BulkWriteOptions: Options
Constructors¶
BulkWrite¶
constructor(context: BsonContext, globalFilter: FilterQuery<Document>.() -> Unit)
Properties¶
context¶
@LowLevelApi
open val context: BsonContext
The context used to generate this expression.
operations¶
options¶
val options: BulkWriteOptions<Document>
Functions¶
accept¶
@LowLevelApi
@DangerousMongoApi
open override fun accept(node: AvailableInBulkWrite<Document>)
Adds a new Node into the current node.
This method is generally considered unsafe, as it allows inserting any kind of node into the current node. Since this library is about representing database requests, this method allows inserting any kind of operation, without necessarily checking any security or coherence invariants.
Users should only interact with this method when they add a new type of node that doesn't exist in the library. For example, when adding an operator that is missing from the library. In these cases, we highly recommend users to contact the maintainers of KtMongo to ensure the created operator respects all invariants. If possible, upstreaming the operator would be of benefit to all users, and guarantees future bug fixes.
In all other cases, it is expected that implementations of this interface provide methods for each added functionality that are responsible for checking invariants and are safe to call.
For a more detailed explanation of the contract of this method, see Node.
acceptAll¶
Adds any number of nodes into this one.
filtered¶
Declares a filter that is shared between all children operations.
Example¶
Sometimes, we have multiple operations in a single bulk write that share the same filter. This method allows to declare it a single time.
freeze¶
@LowLevelApi
open override fun freeze()
Makes this node immutable.
insertMany¶
fun insertMany(documents: Iterable<Document>, options: InsertOneOptions<Document>.() -> Unit = {})
Inserts multiple documents in a single operation.
Example¶
class User(
val name: String,
val age: Int,
)
collection.bulkWrite {
insertMany(listOf(User(name = "Bob", age = 18), User(name = "Alice", age = 17)))
}
External resources¶
See also
-
BulkWrite.insertOne: Insert a single document. -
BulkWrite.updateMany: Update multiple documents.
fun insertMany(vararg documents: Document, options: InsertOneOptions<Document>.() -> Unit = {})
Inserts multiple documents in a single operation.
Example¶
class User(
val name: String,
val age: Int,
)
collection.bulkWrite {
insertMany(User(name = "Bob", age = 18), User(name = "Alice", age = 17))
}
External resources¶
See also
-
BulkWrite.insertOne: Insert a single document. -
BulkWrite.updateMany: Update multiple documents.
insertOne¶
fun insertOne(document: Document, options: InsertOneOptions<Document>.() -> Unit = {})
Inserts a document.
Example¶
class User(
val name: String,
val age: Int,
)
collection.bulkWrite {
insertOne(User(name = "Bob", age = 18))
}
External resources¶
See also
-
BulkWrite.insertMany: Insert multiple documents. -
BulkWrite.updateOne: Update an existing document. -
BulkWrite.upsertOne: Update or insert a document.
replaceOne¶
fun replaceOne(
options: ReplaceOptions<Document>.() -> Unit = {},
filter: FilterQuery<Document>.() -> Unit = {},
document: Document
)
Replaces a document that matches filter by document.
If multiple documents match filter, only the first one found is updated.
Example¶
class User(
val name: String,
val age: Int,
)
collection.bulkWrite {
replaceOne(
filter = { User::name eq "Patrick" },
document = User("Bob", 15)
)
}
External resources¶
See also
-
BulkWrite.updateOne: Update an existing document. -
BulkWrite.repsertOne: Replace a document, or insert it if it doesn't exist.
repsertOne¶
fun repsertOne(
options: ReplaceOptions<Document>.() -> Unit = {},
filter: FilterQuery<Document>.() -> Unit = {},
document: Document
)
Replaces a document that matches filter by document.
If multiple documents match filter, only the first one found is updated.
If no documents match filter, document is inserted.
Example¶
class User(
val name: String,
val age: Int,
)
collection.bulkWrite {
repsertOne(
filter = { User::name eq "Patrick" },
document = User("Bob", 15)
)
}
If a document exists that has the name of "Patrick", it is replaced by the new document. If none exist, the document is inserted.
External resources¶
See also
-
BulkWrite.replaceOne: Replace an existing document. -
BulkWrite.insertOne: Always create a new document.
simplify¶
@LowLevelApi
open fun simplify(): AbstractBsonNode?
Returns a simplified (but equivalent) expression to the current expression.
toBson¶
Writes the result of simplifying to a new BSON document.
toString¶
JSON representation of this expression.
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.
Example¶
class User(
val name: String,
val age: Int,
)
collection.bulkWrite {
updateMany(
filter = { User::name eq "Patrick" },
update = {
User::age set 15
}
)
}
External resources¶
See also
BulkWrite.updateOne: Update a single document.
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.
If multiple documents match filter, only the first one found is updated.
Example¶
class User(
val name: String,
val age: Int,
)
collection.bulkWrite {
updateOne(
filter = { User::name eq "Patrick" },
update = {
User::age set 15
}
)
}
External resources¶
See also
-
BulkWrite.updateMany: Update multiple documents. -
BulkWrite.insertOne: Create a new document. -
BulkWrite.upsertOne: Create a document if none are found.
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.
If multiple documents match filter, only the first one found is updated.
If no documents match filter, a new one is created.
Example¶
class User(
val name: String,
val age: Int,
)
collection.bulkWrite {
upsertOne(
filter = { User::name eq "Patrick" },
update = {
User::age set 15
}
)
}
If a document exists that has the name of "Patrick", its age is set to 15. If none exist, a document with name "Patrick" and age 15 is created.
External resources¶
See also
-
BulkWrite.insertOne: Always create a new document. -
BulkWrite.updateOne: Do nothing if no matching documents are found.
writeTo¶
@LowLevelApi
override fun writeTo(writer: BsonFieldWriter)
Writes the result of simplifying this expression into writer.