MongoD...kWrite(3pm)
π MongoDB::BulkWrite β the bulk write interface bestie π
okay so this page is MongoDB::BulkWrite, straight out of the User Contributed Perl Documentation, and it's literally the manual for doing a whole little pile of writes at once instead of one at a time like a peasant. β¨
πΈ VERSION πΈ
version v2.2.2
π SYNOPSIS π
like here's the vibe, no cap:
use Safe::Isa;
use Try::Tiny;
my $bulk = $collection->initialize_ordered_bulk_op;
$bulk->insert_one( $doc );
$bulk->find( $query )->upsert->replace_one( $doc )
$bulk->find( $query )->update( $modification )
my $result = try {
$bulk->execute;
}
catch {
if ( $_->$isa("MongoDB::WriteConcernError") ) {
warn "Write concern failed";
}
else {
die $_;
}
};
π¦ DESCRIPTION π¦
So this class builds up a whole list of write operations to run in bulk, all for a single collection. On a MongoDB 2.6 or later server that has write command support, it lets you group similar operations together and yeet them over to the database in one go, which minimizes network round-trips. it's giving efficiency, it's giving main character energy. π
To kick off a bulk operation, use one of these methods from MongoDB::Collection:
initialize_ordered_bulk_opinitialize_unordered_bulk_op
π Ordered Operations π
With an ordered operations list, MongoDB executes the write operations in the list serially. If an error happens during the processing of one of the write operations, MongoDB will return without processing any remaining write operations in the list. it just stops, periodt.
π Unordered Operations π
With an unordered operations list, MongoDB can execute in parallel, and in a nondeterministic order, the write operations in the list. If an error happens during the processing of one of the write operations, MongoDB will continue to process remaining write operations in the list. she keeps going, she doesn't stop for anybody. π«Ά
β¨ ATTRIBUTES β¨
collection (required)
The MongoDB::Collection where the operations are to be performed. literally where the magic happens.
ordered (required)
A boolean for whether or not operations should be ordered (true) or unordered (false). pick your fighter. π₯
bypassDocumentValidation
A boolean for whether or not operations should bypass document validation. Default is false.
π₯ METHODS π₯
find
$view = $bulk->find( $query_document );
The find method returns a MongoDB::BulkWriteView object that allows write operations like update or remove, constrained by a query document. it's giving "here's your little sandbox, bestie."
A query document is required. Use an empty hashref for no criteria at all:
$bulk->find( {} )->remove; # remove all documents!
ok so that one literally removes ALL the documents, so maybe think before you type that one, bestie. π
An exception will be thrown on error.
insert_one
$bulk->insert_one( $doc );
Queues a document for insertion when execute is called. The document may be a hash reference, an array reference (with balanced key/value pairs) or a Tie::IxHash object. If the document doesn't have an "_id" field, one will be added to the original. she adds it herself, iconic behavior. π
The method has an empty return on success; an exception will be thrown on error.
execute
my $result = $bulk->execute;
# Optional write concern:
my $result = $bulk->execute( $concern );
# With options
my $result = $bulk->execute( $concern, $options );
Executes the queued operations. The order and semantics depend on whether the bulk object is ordered or unordered:
- ordered β operations are executed in order, but operations of the same type (e.g. multiple inserts) may be grouped together and sent to the server. If the server returns an error, the bulk operation will stop and an error will be thrown.
- unordered β operations are grouped by type and sent to the server in an unpredictable order. After all operations are sent, if any errors occurred, an error will be thrown.
When grouping operations of a type, operations will be sent to the server in batches not exceeding 16MiB or 1000 items (for a version 2.6 or later server) or individually (for legacy servers without write command support). know your batch limits, iykyk.
A write concern is optional, and can either take a pre-constructed WriteConcern object, or the arguments to construct one. For info on write concerns, see MongoDB::WriteConcern.
The options argument is an optional hashref which can contain the following values:
"session"β the session to use for these operations. If not supplied, will use an implicit session. For more information seeMongoDB::ClientSession
This method returns a MongoDB::BulkWriteResult object if the bulk operation executes successfully. slay.
Typical errors might include:
"MongoDB::WriteError"β one or more write operations failed"MongoDB::WriteConcernError"β all writes were accepted by a primary, but the write concern failed"MongoDB::DatabaseError"β a command to the database failed entirely
See MongoDB::Error for more on error handling.
NOTE: it is an error to callexecutewithout any operations or to callexecutemore than once on the same bulk object. one and done, bestie, no double dipping. π
π AUTHORS π
- David Golden
<david@mongodb.com> - Rassi
<rassi@mongodb.com> - Mike Friedman
<friedo@friedo.com> - Kristina Chodorow
<k.chodorow@gmail.com> - Florian Ragwitz
<rafl@debian.org>
π COPYRIGHT AND LICENSE π
This software is Copyright (c) 2020 by MongoDB, Inc.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
and that's the whole page, bestie!! π this doc is served up by perl v5.40.1, dated 2025-04-03, and it's the MongoDB::BulkWrite(3pm) page from the User Contributed Perl Documentation. go forth and batch your writes, you absolute icon. ππ¦β¨
π¦ where this page comes from
- debian trixie: libmongodb-perl 2.2.2-3 Β· Apache-2.0
- ubuntu resolute: libmongodb-perl 2.2.2-3 Β· Apache-2.0
- ubuntu stonking: libmongodb-perl 2.2.2-3 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms