MongoD...ng::v1(3pm)
π MongoDB::Upgrading::v1 β the glow-up guide π
okay bestie, so this is a page from the User Contributed Perl Documentation section of your man pages, and it's filed under MongoDB::Upgrading::v1(3pm). It's literally the "here's what changed" doc for going from the v0 driver to the v1 driver. Grab a snack, this one's a journey. π¦
β¨ NAME β¨
MongoDB::Upgrading::v1 β Deprecations and behavior changes from v0 to v1
πΈ VERSION πΈ
version v2.2.2
π DESCRIPTION π
So the v1 driver is a massive glow-up, fr fr. It's a substantial step forward in functionality and consistency. But β and you knew this was coming β there are so many areas where the old API got deprecated or changed in a backward-breaking way. Not a cute little tweak. A whole personality change. π
This doc exists to help you, my dear developer, update your code so you actually account for the API changes from v0 over to v1. It's giving "public service announcement but make it supportive." π«Ά
π RATIONALE π
The driver got changed because the devs wanted certain goals, and honestly? Valid of them. Here's the list:
- consistency (intra-driver) β so many parts of the v0 API were inconsistent, behaving differently from method to method; the v1 API minimizes developer surprises by improving consistency in return types and exception mechanisms.
- consistency (inter-driver) β the "next-generation" MongoDB drivers across all languages are converging on common APIs and common behaviors; this simplifies developer education and support, because cross-language examples will look similar. iykyk. π
- encapsulation β way too many low-level, internal operations were exposed as part of the API, which complicates maintenance work; the v1 API aims to minimize the "public surface" available to developers, so future development can keep up with MongoDB server enhancements faster with less risk of breakage.
- abstraction β lots of v0 methods returned raw server documents for end-user code to inspect, which is brittle the second server responses change over time; the v1 API uses result classes to abstract the details behind standardized accessors. so much cleaner. π
- server compatibility β some new features and behavior changes in the MongoDB server no longer fit the old driver design; the v1 driver transparently supports both old and new servers. iconic behavior honestly.
- portability β the v0 driver had a large dependency tree and a substantial amount of non-portable C code; the v1 driver removes some dependencies and uses widely-used, well-tested CPAN modules in place of custom C code where possible; and it lays the groundwork for a future "pure-Perl optional" driver. π
- round-trippable data β the v0 BSON implementation could easily change data types when round-tripping documents; the v1 driver is designed to round-trip data correctly whenever possible (within the limits of Perl's dynamic typing, which, okay, we can't fight).
π INSTALLATION AND DEPENDENCY CHANGES π
π§ Moo instead of Moose π§
The v1 driver uses Moo instead of Moose. Result: a slightly faster driver and a significantly reduced deep dependency tree. slay.
π SSL and SASL π
The v0 driver required a compiler plus OpenSSL and libgsasl for SSL and SASL support, respectively. The v1 driver instead leans on the CPAN modules IO::Socket::SSL and Authen::SASL for SSL and SASL support, respectively. No compiler drama. π
SSL configuration is now possible via the ssl attribute.
Authentication configuration is described in "AUTHENTICATION" in MongoDB::MongoClient.
π BEHAVIOR CHANGES π
πΈ MongoClient configuration πΈ
new configuration options (bestie there are so many)
Several configuration options have been added, with particular emphasis on adding more granular control of timings and timeout behaviors. Like, very granular. Control freak era, but make it configurable.
"auth_mechanism""auth_mechanism_properties""bson_codec""connect_timeout_ms""heartbeat_frequency_ms""local_threshold_ms""max_time_ms""replica_set_name""read_pref_mode""read_pref_tag_sets""server_selection_timeout_ms""socket_check_interval_ms""socket_timeout_ms"
π¦ replica set configuration π¦
Connecting to a replica set now requires a replica set name, given either with the "replica_set_name" option for MongoDB::MongoClient or with the "replicaSet" option in a connection string. For example:
$client = MongoDB::MongoClient->new(
host => "mongodb://rs1.example.com,rs2.example.com/",
replica_set_name => 'the_set',
);
$client = MongoDB::MongoClient->new(
host => "mongodb://rs1.example.com,rs2.example.com/?replicaSet=the_set"
);
π configuration options changed to read-only π
Configuration options are changing to be immutable, to prevent surprising action-at-a-distance. (Like, changing an attribute value in one part of your code changes it for other parts of the code that did not expect it. Rude.) Going forward, options may be set at MongoDB::MongoClient construction time only.
The following options have changed to be read-only:
"db_name""j""password""ssl""username""w""wtimeout"
Write concern may be overridden at the MongoDB::Database and MongoDB::Collection level during construction of those objects. For more details, see the later section on write concern changes. (Yes bestie, we're getting there, it's part of this saga.)
π mapping between connection string and configuration options π
Many configuration options may be set via a connection string URI in the "host" option. In the v0 driver, the precedence between the connection string and constructor options was completely inconsistent. In the v1 driver, options set via a connection string URI take precedence over options passed to the constructor. This is consistent with other MongoDB drivers (as well as how DBI treats Data Source Names). periodt.
The list of servers and ports, as well as the optional "username", "password" and "db_name" options, come directly from URI structure. Other options are parsed as key-value parameters at the end of the connection string. Here's how connection string keys map to configuration options in the MongoDB::MongoClient:
authMechanismβauth_mechanismauthMechanismPropertiesβauth_mechanism_propertiesconnectTimeoutMSβconnect_timeout_msheartbeatFrequencyMSβheartbeat_frequency_msjournalβjlocalThresholdMSβlocal_threshold_msmaxTimeMSβmax_time_msreadPreferenceβread_pref_modereadPreferenceTagsβread_pref_tag_setsreplicaSetβreplica_set_nameserverSelectionTimeoutMSβserver_selection_timeout_mssocketCheckIntervalMSβsocket_check_interval_mssocketTimeoutMSβsocket_timeout_mssslβsslwβwwTimeoutMSβwtimeout
The "readPreferenceTags" and "authMechanismProperties" keys take colon-delimited, comma-separated pairs. so:
readPreferenceTags=dc:nyeast,rack:1
authMechanismProperties=SERVICE_NAME:mongodb
The "readPreferenceTags" option may be repeated to build up a list of tag set documents:
readPreferenceTags=dc:nyc,rack:1&readPreferenceTags=dc:nyc
π deprecated configuration options π
Several options have been superseded, replaced or renamed for clarity, and are therefore deprecated and undocumented. They're kept around for a limited degree of backwards compatibility, and they will generally be used as fallbacks for other options. If any of them were read-write, they've also been changed to read-only. rip to them.
"dt_type"β see "BSON encoding changes" for details."query_timeout"β replaced by"socket_timeout_ms"; if set, this will be used as a fallback default for"socket_timeout_ms"."sasl"β superseded by"auth_mechanism"; if set, this will be used along with"sasl_mechanism"as a fallback default for"auth_mechanism"."sasl_mechanism"β superseded by"auth_mechanism"; if set, this will be used as a fallback default for"auth_mechanism"."timeout"β replaced by"connect_timeout_ms"; if set, this will be used as a fallback default for"connect_timeout_ms".
These will be removed in a future major release. So like, don't build your whole personality on them, bestie. π
π configuration options removed π
Some configuration options have been removed entirely, because they no longer serve any purpose given changes to server discovery, server selection and connection handling:
"auto_connect""auto_reconnect""find_master""max_bson_size"
And as described further below in the "BSON encoding changes" section, these BSON encoding configuration options have been removed as well:
"inflate_dbrefs""inflate_regexps"
Removed configuration options will be ignored if passed to the MongoDB::MongoClient constructor. they simply do not hear you. π
β lazy connections and reconnections on demand β
The improved approach to server monitoring and selection allows all connections to be lazy. When the client is constructed, no connections are made until the first network operation is needed. At that time, the client will scan all servers in the seed list and begin regular monitoring. Connections that drop will be re-established when needed. self-care era.
IMPORTANT: Code that used to rely on a fatal exception from MongoDB::MongoClient->new when no mongod is available will break. Instead, users are advised to just conduct their operations and be prepared to handle errors.
For testing, users may wish to run a simple command to check that a mongod is ready:
use Test::More;
# OLD WAY: BROKEN
plan skip_all => 'no mongod' unless eval {
MongoDB::MongoClient->new
};
# NEW WAY 1: with MongoDB::MongoClient
plan skip_all => 'no mongod' unless eval {
MongoDB::MongoClient->new->db('admin')->run_command(
[ ismaster => 1 ]
)
};
# NEW WAY 2: with MongoDB and connect
plan skip_all => 'no mongod' unless eval {
MongoDB->connect->db('admin')->run_command([ ismaster => 1 ])
};
See SERVER SELECTION and SERVER MONITORING AND FAILOVER in MongoDB::MongoClient for details. π
π₯ exceptions are the preferred error handling approach π₯
In the v0 driver, errors could be indicated in so many different ways, and genuinely bestie, pick a lane:
- boolean return value
- string return value is an error; hash ref is success
- document that might contain an
'err','errmsg'or'$err'field - thrown string exception
And regardless of the documented error handling, every method that involved a network operation would throw an exception on various network errors. so the docs said one thing and the code did another. classic.
In the v1 driver, exception objects are the standard way of indicating errors. The exception hierarchy is described in MongoDB::Error.
β¨ Cursors and query responses β¨
Okay so back in v0, MongoDB::Cursor objects were doing the absolute most β they were used for ordinary queries and the query-like commands aggregation and parallel scan. But bestie, only cursor iteration commands worked for aggregation and parallel scan βcursorsβ; the rest of the MongoDB::Cursor API didnβt apply and was fatal. π
In v1, all result iteration is done via the new MongoDB::QueryResult class. MongoDB::Cursor is now just a thin wrapper that holds query parameters, instantiates a MongoDB::QueryResult on demand, and passes iteration methods through to the query result object.
This significantly simplifies the code base and should have little end-user visibility unless users are specifically checking the return type of queries and query-like methods.
The explain cursor method no longer resets the cursor.
The slave_okay cursor method now sets the read_preference to 'secondaryPreferred' or clears it to 'primary'.
The snapshot cursor method now requires a boolean argument, allowing it to be turned on or off before executing the query. Calling it without an argument (as it was in v0) is a fatal exception.
Parallel scan βcursorsβ are now QueryResult objects, with the same iteration methods as in v0.
The $MongoDB::Cursor::slave_okay global variable has been removed as part of the revision to read preference handling. See the read preferences section below for more details.
The $MongoDB::Cursor::timeout global variable has also been removed. Timeouts are set during MongoDB::MongoClient configuration and are immutable. See the section on configuration changes for more.
πΈ Aggregation API πΈ
On MongoDB 2.6 or later, aggregate always uses a cursor to execute the query. The batchSize option has been added (but has no effect prior to 2.6). The cursor option is deprecated.
The return types for the aggregate method are now always QueryResult objects, regardless of whether the aggregation uses a cursor internally or is an 'explain'.
NOTE: To help users with a 2.6mongosand mixed version shards with versions before 2.6, passing the deprecated'cursor'option with a false value will disable the use of a cursor. This workaround is provided for convenience and will be removed when 2.4 is no longer supported.
π Read preference objects and the read_preference method π
A new MongoDB::ReadPreference class is used to encapsulate read preference attributes. In the v1 driver, it is constructed from the read_pref_mode and read_pref_tag_sets attributes on MongoDB::MongoClient:
MongoDB::MongoClient->new(
read_pref_mode => 'primaryPreferred',
read_pref_tag_sets => [ { dc => 'useast' }, {} ],
);
The old read_preference method to change the read preference has been removed and trying to set a read preference after the client has been created is a fatal error. The old mode constants PRIMARY, SECONDARY, etc. have been removed.
The read_preference method now returns the MongoDB::ReadPreference object generated from read_pref_mode and read_pref_tag_sets.
It is inherited by MongoDB::Database, MongoDB::Collection, and MongoDB::GridFS objects unless provided as an option to the relevant factory methods:
my $coll = $db->get_collection(
"foo", { read_preference => 'secondary' }
);
Such read_preference arguments may be a MongoDB::ReadPreference object, a hash reference of arguments to construct one, or a string that represents the read preference mode.
MongoDB::Database and MongoDB::Collection also have clone methods that allow easy alteration of a read preference for a limited scope.
my $coll2 = $coll->clone( read_preference => 'secondaryPreferred' );
For MongoDB::Cursor, the read_preference method sets a hidden read preference attribute that is used for the query in place of the MongoDB::MongoClient default read_preference attribute. This means that calling read_preference on a cursor object no longer changes the read preference globally on the client β the read preference change is scoped to the cursor object only. periodt.
π Write concern objects and removing the safe argument π
A new MongoDB::WriteConcern class is used to encapsulate write concern attributes. In the v1 driver, it is constructed from the w, wtimeout and j attributes on MongoDB::MongoClient:
MongoDB::MongoClient->new( w => 'majority', wtimeout => 1000 );
The write_concern method now returns the MongoDB::WriteConcern object generated from w, wtimeout and j.
It is inherited by MongoDB::Database, MongoDB::Collection, and MongoDB::GridFS objects unless provided as an option to the relevant factory methods:
$db = $client->get_database(
"test", { write_concern => { w => 'majority' } }
);
Such write_concern arguments may be a MongoDB::WriteConcern object, a hash reference of arguments to construct one, or a string that represents the w mode.
MongoDB::Database and MongoDB::Collection also have clone methods that allow easy alteration of a write concern for a limited scope.
my $coll2 = $coll->clone( write_concern => { w => 1 } );
The safe argument is no longer used in the new CRUD API.
π¦ Authentication based only on configuration options π¦
Authentication now happens automatically on connection during the handshake with any given server based on the auth_mechanism attribute.
The old authenticate method in MongoDB::MongoClient has been removed.
π Bulk API π
π Bulk method names changed to match CRUD API π
Method names match the new CRUD API, e.g. insert_one instead of insert and so on. The legacy names are deprecated.
π Bulk insertion π
Insertion via the bulk API will NOT insert an _id into the original document if one does not exist. Previous documentation was not specific whether this was the case or if the _id was added to the document sent to the server.
π Bulk write results π
The bulk write results class has been renamed to MongoDB::BulkWriteResult. It keeps MongoDB::WriteResult as an empty superclass for some backwards compatibility so that $result->isa("MongoDB::WriteResult") will continue to work as expected.
The attributes have been renamed to be consistent with the new CRUD API. The legacy names are deprecated, but are available as aliases.
π GridFS π
The MongoDB::GridFS class now has explicit read preference and write concern attributes inherited from MongoDB::MongoClient or MongoDB::Database, just like MongoDB::Collection. This means that GridFS operations now default to an acknowledged write concern, just like collection operations have been doing since v0.502.0 in 2012.
The use of safe is deprecated.
Support for ancient, undocumented positional parameters circa 2010 has been removed.
π Low-level functions removed π
Low-level driver functions have been removed from the public API.
π MongoDB::Connection removed π
The MongoDB::Connection module was deprecated in v0.502.0 and has been removed.
π§ BSON encoding changes π§
In the v1 driver, BSON encoding and decoding have been encapsulated into a MongoDB::BSON codec object. This can be provided at any level, from MongoDB::MongoClient to MongoDB::Collection. If not provided, a default will be created that behaves similarly to the v0 encoding/decoding functions, except for the following changes.
π $MongoDB::BSON::use_binary removed π
Historically, this defaulted to false, which corrupts binary data when round tripping. Retrieving a binary data element and re-inserting it would have resulted in a field with UTF-8 encoded string of binary data.
Going forward, binary data will be returned as a MongoDB::BSON::Binary object. A future driver may add the ability to control decoding to allow alternative representations.
π $MongoDB::BSON::use_boolean removed π
This global variable never worked. BSON booleans were always deserialized as boolean objects. A future driver may add the ability to control boolean representation.
π $MongoDB::BSON::utf8_flag_on removed π
In order to ensure round-tripping of string data, this variable is removed. BSON strings will always be decoded to Perl character strings. Anything else risks double-encoding a round-trip.
π $MongoDB::BSON::looks_like_number and $MongoDB::BSON::char deprecated and re-scoped π
In order to allow a future driver to provide more flexible user-customized encoding and decoding, these global variables are deprecated. If set, they will be examined during MongoDB::MongoClient->new() to set the configuration of a default MongoDB::BSON codec (if one is not provided). Changing them later will NOT change the behavior of the codec object.
π MongoDB::MongoClient option inflate_regexps removed π
Previously, BSON regular expressions decoded to qr{} references by default and the MongoDB::MongoClient inflate_regexps option was available to decode instead to MongoDB::BSON::Regexps.
Going forward in the v1.0.0 driver, for safety and consistency with other drivers, BSON regular expressions always decode to MongoDB::BSON::Regexp objects.
π MongoDB::MongoClient option inflate_dbrefs removed π
The inflate_dbrefs configuration option has been removed and replaced with a dbref_callback option in MongoDB::BSON.
By default, the MongoDB::MongoClient will create a MongoDB::BSON codec that will construct MongoDB::DBRef objects. This ensures that DBRefs properly round-trip.
π MongoDB::MongoClient option dt_type deprecated and changed to read-only π
The dt_type option is now only takes effect if MongoDB::MongoClient constructs a MongoDB::BSON codec object. It has been changed to a read-only attribute so that any code that relied on changing dt_type after constructing a MongoDB::MongoClient object will fail instead of being silently ignored.
π Int32 vs Int64 encoding changes π
On 64-bit Perls, integers that fit in 32-bits will be encoded as BSON Int32 (whereas previously these were always encoded as BSON Int64).
Math::BigInt objects will always be encoded as BSON Int64, which allows users to force 64-bit encoding if desired.
π Added support for Time::Moment π
Time::Moment is a much faster replacement for the venerable DateTime module. The BSON codec will serialize Time::Moment objects correctly and can use that module as an argument for the dt_type codec attribute.
π Added support for encoding common JSON boolean classes π
Most JSON libraries on CPAN implement their own boolean classes. The following libraries boolean types will now encode correctly as BSON booleans:
JSON::XSCpanel::JSON::XSJSON::PPJSON::TinyMojo::JSON
π DBRef objects π
The fetch method and related attributes client, verify_db, and verify_coll have been removed from MongoDB::DBRef. π
okay so real talk: providing a fetch method was inconsistent with other MongoDB drivers, which either never provided it, or have dropped it in the next-generation drivers. It requires a client attribute, which tightly couples BSON decoding to the client model, causing circular reference issues and triggering Perl memory bugs under threads. So the v1.0.0 driver no longer support fetching directly from MongoDB::DBRef; users will need to implement their own methods for dereferencing. π
Also, the db attribute is now optional, consistent with the specification for DBRefs. And all attributes (ref, id and db) are now read-only, consistent with the move toward immutable objects throughout the driver. π
To support round-tripping DBRefs with additional fields other than $ref, $id and $db, the DBRef class now has an attribute called extra. But heads up: as not all drivers support this feature, using it for new DBRefs is not recommended. π
π DEPRECATED METHODS π
Deprecated options and methods may be removed in a future release. Their documentation has been removed to discourage ongoing use. Unless otherwise stated, they will continue to behave as they previously did, allowing a degree of backwards compatibility until code is updated to the new MongoDB driver API. π
MongoDB::Database
evalβ MongoDB 3.0 deprecated the$evalcommand, so this helper method is deprecated as well.last_errorβ Errors are now indicated via exceptions at the time database commands are executed.
MongoDB::Collection
insert,batch_insert,remove,update,save,queryandfind_and_modifyβ A new common driver CRUD API replaces these legacy methods.get_collectionβ This method implied that collections could be contained inside collection. This doesn't actually happen, so it's confusing to have a Collection be a factory for collections. Users who want nested namespaces should be explicit and create them off Database objects instead.ensure_index,drop_indexes,drop_index,get_indexβ A newMongoDB::IndexViewclass is accessible through theindexesmethod, offering greater consistency in behavior across drivers.validateβ The return values have changed over different server versions, so this method is risky to use; it has more use as a one-off tool, which can be accomplished viarun_command.
MongoDB::CommandResult
resultβ has been renamed tooutputfor clarity.
MongoDB::Cursor
slave_okβ this modifier method is superseded by theread_preferencemodifier method.countβ this is superseded by theMongoDB::Collection#countin MongoDB::Collection count method. Previously, this ignored skip/limit unless a true argument was passed, which was a bizarre, non-intuitive and inconsistent API. π΅βπ«
MongoDB::BulkWrite and MongoDB::BulkWriteView
insertβ renamed toinsert_onefor consistency with CRUD API.updateβ renamed toupdate_manyfor consistency with CRUD API.removeβ renamed todelete_manyfor consistency with CRUD API.remove_oneβ renamed todelete_onefor consistency with CRUD API.
π 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 part came from perl v5.40.1, dated 2025-04-03, and the page itself is MongoD...ng::v1(3pm). slay, you made it to the end π
β¨
π¦ 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