Migrating Your Application To 2.0
There are some key changes in the driver going from 1.X to 2.X that one needs to be aware off before changing your application to using the new 2.X versions. There has been some cleanup of API’s and some deprecations of 1.X features.
Design differences in 2.X
One of the main changes is that the driver has been split into two pieces. There is a new
mongodb-core that contains the low level MongoDB API’s while
mongodb contains the high level driver.
mongodb-core is targeted to creators of libraries like
Mongoose and other ODM’s who do not need the abstractions available in the
mongodb driver. The driver is also as of the time of writing compatible with the 0.11.x node.js branch including Kerberos support as well as MongoDB 2.8.
We will outline where changes have occurred that could break your existing application.
Node.JS versions and Streams
The 2.0 driver drops support for 0.8.x style streams and moves to 0.10.x or higher style pull based streams making for more reliable and faster streams. Backwards compatibility is by using the
readable-stream npm package that might cause some slight behavior changes for the cursor streams.
All dependencies have now been updated to use the
nan package meaning they will compile and work on 0.11.x or higher.
The grid object has been removed as it’s not widely used and offers very limited GridStore capabilities.
The db instance object have had several changes made to it. We’ve removed the following methods.
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 2.8 or higher alternative storage enginers.
Added the following method
db.listCollectionsto replace all other collection inquiry method as it will do the correct thing for MongoDB 2.8 and higher as well as provide backwards compatibility for MongoDB 2.6 or lower.
A collection instance has also had several changes made to it. Most importantly we now return the
mongodb-core result objects directly with all the associated information returned from the server instead of the current selective information returned in the 1.4.x version.
We’ve added the following new methods
collection.insertOneInsert a single document.
collection.insertManyInsert an array of documents.
collection.replaceOneReplace an existing document fully.
collection.updateOneUpdate a single document.
collection.updateManyUpdate multiple documents in one go.
collection.deleteOneDelete a single document.
collection.deleteManyDelete multiple documents in one go.
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 3 methods now also return the full
mongodb-core results and have had their third return value removed to ensure less 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 or lower this means a max 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 existing behavior from 1.4 so no code should break but the API documentation reflects the new preferred way to use the find to execute queries.
The GridStore object has had some major changes due to issues discovered by users related to parallel writing using the previous available
w+ append mode. Thus
w+ in 2.0 only allows for changes to the file metadata and does not allow for appending to a file avoiding the possible data corruption. The hope is to create a new GridStore spec in the future that allows for proper handling parallel writing to an existing file but this requires changes for all drivers as well as the server.
MongoClient now only has the class method
connect. Constructing of a new MongoClient using
Mongos has been removed due to the confusion it caused in 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.