Please help us improve by answering a few questions at our user survey.
- Upgrade Guide
- Upgrading to 2.x
Migrating Your Application To 2.x
There are some key changes in the driver that you should be aware of before migrating your application from version 1.x to 2.x. Some APIs are cleaned up, and some 1.x features have been deprecated.
Design differences in 2.x
One major change is that the driver has been split into two pieces. There is a new
mongodb-core that contains the
low-level MongoDB APIs while
mongodb contains the high-level driver.
mongodb-core is targeted to creators of libraries like Mongoose and other ODMs who do not need the abstractions available in the
mongodb driver. The driver is
currently compatible with the 0.11.x Node.js branch, which includes support for Kerberos and MongoDB 3.0.
Below are listed some driver changes which could impact your application.
Node.js versions and Streams
The 2.0 driver drops support for 0.8.x style streams in favor of 0.10.x or higher style pull-based streams, which are
faster and more reliable. Backwards compatibility is available by using the
readable-stream npm package (might cause
some behavior changes for the cursor streams).
All dependencies have now been updated to use the
nan package. They will compile and work on 0.11.x or higher.
The grid object has been removed, due to its limited GridStore capabilities.
The db instance object has changed in several ways. The following methods have been removed:
db.dereferencedue to db references being deprecated in the server.
db.cursorInforemoved as it never worked reliably.
db.statsremoved as inconsistent.
db.collectionNamesremoved as it’s just a specialized version of the new
db.collectionInforemoved as it’s not compatible with the new MongoDB 3.0 or higher alternative storage engines.
db.listCollectionsto replace all other collection inquiry methods. It works with MongoDB 3.0 and higher and provide backwards compatibility for MongoDB 2.6 or lower.
The collection instance object has also changed in several key respects. Most importantly, we now return the
mongodb-core result objects directly, with all the associated information returned from the server, instead of the selective information returned in the 1.4.x version.
collection.insertOneinsert a single document.
collection.insertManyinsert an array of documents.
collection.replaceOnefully replace an existing document.
collection.updateOneupdate a single document.
collection.updateManyupdate multiple documents.
collection.deleteOnedelete a single document.
collection.deleteManydelete multiple documents.
collection.findOneAndUpdateuse findAndModify to update a document.
collection.findOneAndDeleteuse findAndModify to remove a specific document.
collection.findOneAndReplaceuse findAndModify to replace a specific document.
remove methods are marked for deprecation and will be removed in a future 3.0 driver. These three methods now return the full
mongodb-core results, and their third return value has been removed to ensure fewer compatibility problems with orchestration libraries like
The insert methods are now capping at the
maxWriteBatchSize passed back from MongoDB on the results from the
ismaster command. For MongoDB 2.4 and lower this means a maximum of 1000 documents in each insert batch. Legacy insert mode has been deprecated in favor of proper emulation of current 2.6 or higher write commands.
Another important change is in how
collection.find works. The idea is to chain commands instead of passing them into the
find method. It still supports old behavior from 1.4 so no code should break, but the API documentation reflects the new, preferred way of using
find to execute queries.
The GridStore object has changed in major ways, due to issues discovered by users related to parallel writing (using the previously available
w+ append mode). As a result, in 2.0
w+ only allows for changes to the file metadata and does not allow for appending to a file, avoiding possible data corruption. The hope is to create a new GridStore spec in the future that allows for properly handling parallel writing to an existing file, but that will require changes for all drivers as well as the server.
MongoClient now has only the class method
connect. Construction of a new MongoClient using
Mongos has been removed, due to the confusion it caused by duplicating the way one can build a topology connection using
Db in 1.4.
MongoClient.connect is the recommended way to connect to a MongoDB topology.