Fork me on GitHub

Migrating from 3.x to 4.x

There are several backwards-breaking changes to be aware of when migrating from Mongoose 3 to Mongoose 4.

findOneAndUpdate() new field is now `false` by default

Mongoose's findOneAndUpdate() and findOneAndRemove() functions are just wrappers around MongoDB's findAndModify command. Both the MongoDB server and the MongoDB NodeJS driver set the new option to false by default, but mongoose 3 overwrote this default. In order to be more consistent with the MongoDB server's documentation, mongoose will use false by default. That is, findOneAndUpdate({}, { $set: { test: 1 } }, callback); will return the document as it was before the $set operation was applied.

To return the document with modifications made on the update, use the new: true option.

MyModel.findOneAndUpdate({}, { $set: { test: 1 } }, { 'new': true }, callback);

CastError and ValidationError now use `kind` instead of `type` to report error types

In Mongoose 3, CastError and ValidationError had a type field. For instance, user defined validation errors would have a type property that contained the string 'user defined'. In Mongoose 4, this property has been renamed to kind due to the V8 JavaScript engine using the Error.type property internally.

More Info

Related blog posts: