Skip to content


Subversion checkout URL

You can clone with
Download ZIP

3.7 (unstable) Release Notes

Aaron Heckmann edited this page · 148 revisions

Beginning with this release, the Mongoose project has moved to a release versioning approach similar to the MongoDB and Node.js projects. All odd numbered minor versions are to be considered unstable where you should expect larger, potentially breaking changes to occur during iteration towards the next even numbered (stable) release.

We feel this approach provides a cleaner, more consistent way to describe and iterate on new code branches than tagging pre-releases as "beta0", "beta1", etc.


  • X.OddNumber.X is an unstable release
  • X.EvenNumber.X is a stable release

For example: 3.7.0, 3.7.1 and 4.11.398 are unstable, whereas 3.8.0, 3.8.1, and 3.20.1 are stable.

For more information on this versioning scheme, please see the MongoDB example.

The nitty-gritty

Mongoose 3.7 brings with it some significant changes and improvements under the hood:

  • [added: geoSearch support](./3.7-(unstable)-Release-Notes#geosearch)
  • [added: geoNear support](./3.7-(unstable)-Release-Notes#geonear)
  • [added: $geoWithin support (important if running MongoDB < 2.4)](./3.7-(unstable)-Release-Notes#geowithin-changes)
  • [added: connection pool sharing](./3.7-(unstable)-Release-Notes#connection-pool-sharing)
  • [added: model.update() now has an overwrite option](./3.7-(unstable)-Release-Notes#modelupdate-now-supports-overwrite)
  • [added: MongooseBuffer#subtype()](./3.7-(unstable)-Release-Notes#mongoosebuffersubtype)
  • [added: awaitdata support](./3.7-(unstable)-Release-Notes#awaitdata-support)
  • [added: stand-alone custom base queries support](./3.7-(unstable)-Release-Notes#querytoconstructor)
  • [added; query.remove(conditions, callback) support](./3.7-(unstable)-Release-Notes#queryremove-behavior-change)
  • [added; GeoJSON support for query.near()](./3.7-(unstable)-Release-Notes#geojson-support-for-querynear)
  • [added: aggregation builder](./3.7-(unstable)-Release-Notes#aggregation-builder)
  • [added: custom error message support for built-in validators](./3.7-(unstable)-Release-Notes#custom-error-message-support-for-built-in-validators)
  • [added: option to disable collection name pluralization](./3.7-(unstable)-Release-Notes#pluralization-optional)
  • [changed: no longer support node 0.6.x](./3.7-(unstable)-Release-Notes#remove-node-06x-support)
  • [changed: Model.aggregate() API](./3.7-(unstable)-Release-Notes#modelaggregate-arguments)
  • [changed: new query builder (mquery)](./3.7-(unstable)-Release-Notes#mquery)
    • [within() changes](./3.7-(unstable)-Release-Notes#within-changes)
    • [intersects() changes](./3.7-(unstable)-Release-Notes#intersects-changes)
    • [geometry() changes](./3.7-(unstable)-Release-Notes#querygeometry-no-longer-accepts-path)
    • [method chain validation (will error with invalid method combinations)](./3.7-(unstable)-Release-Notes#validation-against-nonsensical-queries)
  • [changed: document.remove callback arguments](./3.7-(unstable)-Release-Notes#documentremove-callback-arguments)
  • [changed: document.toObject() now respects child schema transform options](./3.7-(unstable)-Release-Notes#child-schemas-transform-options-now-respected)
  • [changed: document.toJSON() now respects child schema and populated documents toJSON options](./3.7-(unstable)-Release-Notes#subdoc-and-populated-docs-tojson-options-now-respected)
  • [changed: disabling safe mode now disables versioning](./3.7-(unstable)-Release-Notes#disabling-safe-mode-now-also-disables-versioning-support)
  • [changed: no longer pluralizing collection names ending with a non-alphabetic](./3.7-(unstable)-Release-Notes#pluralization-no-longer-applied-to-non-alphabetic-characters)
  • [changed: required Mixed paths now validate properly](./3.7-(unstable)-Release-Notes#mixed-types-now-support-being-required)
  • [changed: toObject/toJSON no longer ignore minimize option](./3.7-(unstable)-Release-Notes#toobjecttojson-no-longer-ignore-minimize-option)
  • [deprecated: query#nearSphere - use Query#near() with the spherical option instead](./3.7-(unstable)-Release-Notes#nearsphere-deprecated)
  • [deprecated: Query#center - use Query#circle() instead.](./3.7-(unstable)-Release-Notes#querycenter-is-deprecated)
  • [deprecated: Query#centerSphere - use Query#circle() instead.](./3.7-(unstable)-Release-Notes#querycentersphere-is-deprecated)
  • [deprecated: Query#slaveOk](./3.7-(unstable)-Release-Notes#queryslaveok-is-deprecated)
  • [deprecated: promise#addBack](./3.7-(unstable)-Release-Notes#promiseaddback-is-deprecated)
  • [deprecated: promise#addCallback](./3.7-(unstable)-Release-Notes#promiseaddcallback-is-deprecated)
  • [deprecated: promise#addErrback](./3.7-(unstable)-Release-Notes#promiseadderrback-is-deprecated)
  • [deprecated: promise#complete](./3.7-(unstable)-Release-Notes#promisecomplete-is-deprecated)
  • [deprecated: Inferred unsafe Query#update/remove](/3.7-(unstable)-Release-Notes#queryupdate-and-queryremove-only-executes-with-callback-or-explicit-true)


Mongoose now supports new geoSearch command in MongoDB.

To use geoSearch, you must have a haystack index defined, which can be done as shown below:

var schema = new Schema({
  pos : [Number],
  complex : {},
  type: String

schema.index({ "pos" : "geoHaystack", type : 1},{ bucketSize : 1});
mongoose.model('Geo', schema);

You can use the geoSearch command much like you would use mapreduce().

Geo.geoSearch({ type : "place" }, { near : [9,9], maxDistance : 5 }, function (err, results, stats) {

For information on the required options and parameters, please see the MongoDB docs


We now have support for the geoNear command.

To use geoNear, your model must have a 2d or 2dsphere index. For more information on these, see the MongoDB docs

var schema = new Schema({
  pos : [Number],
  type: String

schema.index({ "pos" : "2dsphere"});

mongoose.model('Geo', schema);

Once that has been defined, you can access the geoNear method on the model.

Geo.geoNear([9,9], { spherical : true, maxDistance : .1 }, function (err, results, stats) {

For more information on the geoNear command and required options, see the MongoDB docs


mquery is a fluent query building library for MongoDB. It's API is very similar to Mongoose's old query syntax, but it does have some improvements and is much more standardized. Any query you can construct with mquery, can now be constructed with Mongoose using the same syntax. At the same time, all old Mongoose syntax that mquery does not support should still be available (with 2 notable exceptions, see below). If any old syntax (that was using public APIs-- we do not guarantee support for any internal APIs used, and they have changed) does not work, please submit a report.

This change reduces the size of the Mongoose code base by a couple thousand lines. Additionally, it allows for you to integrate your own query engine should you desire.

within() changes

In 3.6.x, a within query was constructed like this:


The syntax has now changed to:


A shim to revert these changes and return to the old syntax can be found here

intersects() changes

In 3.6.x, a within query was constructed like this:


The syntax has now changed to:


A shim to revert these changes and return to the old syntax can be found here

Query#geometry no longer accepts path

geometry no longer accepts a path argument. This makes the Mongoose query building much more semantically consistent.

A query like this:

Geo.within().geometry('loc', { type: 'Polygon', coordinates : coords });

Now turns into:

Geo.where('loc').within().geometry({ type: 'Polygon', coordinates : coords });

geoWithin changes

If you are running a version of MongoDB < 2.4 this affects you.

In MongoDB 2.4, $within was deprecated and $geoWithin was introduced which is 100% backward compatible with $within. For .within() queries, we now internally use $geoWithin by default. However, you can change this to remain backward compatible with old releases of MongoDB and force Mongoose to continue using $within by setting a flag. To toggle this flag, simply change the use$geoWithin property on the Query object.

var mongoose = require('mongoose');
mongoose.Query.use$geoWithin = false;

aggregation builder

There is now an API for building aggregation pipelines, courtesy of njoyard. It works very similar to query building in Mongoose.


Model.aggregate({ $match: { age: { $gte: 21 }}}).unwind('tags').exec(cb);

All items returned will be plain objects and not mongoose documents.

Supported Operations


model.aggregate arguments

The Model.aggregate() argument signature has changed; now no longer accepting an options argument. Now that we are wrapping the driver's aggregate method, we can provide a cleaner, less error prone approach through the use of the new aggregation builders read method.

// old way of specifying the read preference option
Model.aggregate({ $match: {..}}, .. , { readPreference: 'primary' }, callback);

// new approach
Model.aggregate({ $match: {..}}, ..).read('primary').exec(callback)


Say you have a common set of criteria you query for repeatedly throughout your application, like selecting only accounts that have not made a payment in the past 30 days:

Accounts.find({ lastPayment: { $lt: 30DaysAgo }}).select('name lastPayment').exec(callback)

To make managing these common sets of query criteria easier, Mongoose now supports creating reusable base queries.

var Late = Accounts.find({ lastPayment: { $lt: 30DaysAgo }}).select('name lastPayment').toConstructor();

Now, utilizing the Late query constructor, we can clean up our codebase a bit:


Since Late is a query constructor, not just an instance, we can make modifications to the returned query instance without any impacts to the original it was based on.

Late().where({ active: true }).lean().exec(callback)

And since Late is a stand-alone subclass of mongoose.Query, we can make customizations to the constructor itself without impacting any global queries either.

Late.prototype.startsWith = function (prefix) {
  this.where({ name: new RegExp('^' + prefix) });
  return this;

Custom error message support for built-in validators

All built-in validators now support error message customization. Error messages can be defined in the schema or globally.

// in the schema
new Schema({ x: { type: Number, min: [0, '{VALUE} is less than the minimum required ({MIN}) for path: {PATH}'] }})

// globally
var messages = require('mongoose').Error.messages;
messages.String.enum  = "Your custom message for {PATH}.";

As seen in the above example, error messages now also support templating (if that's really a term):

var schema = new Schema({ name: { type: String, match: [/^T/, '{PATH} must start with "T". You provided: `{VALUE}`'] }});
var M = mongoose.model('M', schema);
var m = new M({ name: 'Nope' }); (err) {
  // prints..
  // { message: 'Validation failed',
  //   name: 'ValidationError',
  //   errors:
  //   { name:
  //     { message: 'name must start with "T". You provided: `Nope`',
  //       name: 'ValidatorError',
  //       path: 'name',
  //       type: 'regexp',
  //       value: 'Nope' } } }

The following tokens are replaced as follows:

{PATH} is replaced with the invalid document path {VALUE} is replaced with the invalid value {TYPE} is replaced with the validator type such as "regexp", "min", or "user defined" {MIN} is replaced with the declared min value for the Number.min validator {MAX} is replaced with the declared max value for the Number.max validator

One change to be aware of for custom validators is that in previous versions, the error message was assigned to the type property of the ValidationError. Going forward, the type property is assigned the value user defined.

function validator () { return false }

Schema({ name: { type: String, validate: validator }}) (err) {
  console.log(err.type) // 'user defined'

See issue #747

GeoJSON Support for query.near()

The mongoose query builders near() method now supports passing GeoJSON objects as well:

Test.where('loc').near({ center: { type: 'Point', coordinates: [11,20]}, maxDistance : 1000000 })

query.remove() behavior change

In Mongoose < 3.7.1, Query#remove() always executed the operation and did not accept query conditions. To be consistent with other Query operations, query conditions and/or callback are now accepted. The operation is executed only if the callback is passed.

query.remove(conds, fn); // executes
query.remove(conds) // no execution
query.remove(fn) // executes
query.remove() // no execution

To execute a remove query without passing a callback, use remove() followed by exec().

var promise = query.remove().exec()

child schemas transform options now respected

Before version 3.7.3, when a schema and its subdocument both had transforms defined, the top-level transform was used for the subdocuments as well. This behavior has now been corrected.

See #1412

subdoc and populated docs toJSON options now respected

Before version 3.7.3, subdocuments and populated child documents toJSON options were ignored when calling parent.toJSON() and the parents toJSON options were used instead. This behavior has now been corrected.

See #1376

disabling safe mode now also disables versioning support

When { w: 0 } or safe: false is used in your schema to force non-acknowledged writes (rare but some people use this), no response is received from MongoDB causing an error to occur in the versioning handler. Because mongoose doesn't know if the write was successful under these conditions, versioning must be disabled as well. In 3.7 we now disable it automatically when using non-acknowledged writes.

See #1520

pluralization optional

Mongoose has historically pluralized collection names. While some feel its a nice feature it often has lead to confusion when later exploring the database in the MongoDB shell. This release includes optionally disabling pluralization of collection names.

// disable globally
mongoose.set('pluralization', false)

// disable at the model/collection level
var schema = Schema({..}, { pluralization: false })

Eventually (v4), all pluralization will be removed.

See #1707

pluralization no longer applied to non-alphabetic characters

In previous versions of mongoose, a model named "plan_" would get pluralized to "plan_s". This is no longer the case. Now, if the name ends with a non-alphabetic character, it will not be pluralized.

See #1703

mixed types now support being required

Previously we were unable to make Mixed types required. This has been fixed.

See #1722

toObject/toJSON no longer ignore minimize option

See #1744 and #1607

remove node 0.6.x support

We have discontinued support of running mongoose on node < 0.8.x. While we made no actual code changes related to this announcment, the node project itself isn't supporting it, so neither will we.

connection pool sharing

Mongoose connection objects can now share their underlying connection pool across multiple databases. This helps avoid excessive numbers of open connections when working with multiple databases.

For example, an application working with 5 databases in a 3 node replica-set previously required creating 5 separate mongoose connection instances. Since each mongoose connection instance opens 6 internal connections (by default) to each node of your replica set, a total of 90 (635) connections were opened. Now, utilizing connection.useDb(dbname), we are able to just reuse the existing 18 (6*3) connections.

// create a connection to a database
var db = mongoose.createConnection(uri);

// use another database without creating additional connections
var db2 = db.useDb('someDbName');

// proceed as you would normally
var Model1 = db.model('Model1', m1Schema);
var Model2 = db2.model('Model2', m2Schema);

Since both db and db2 use the same connection pool, when either of them close, both will be closed. Same goes for open() and other events like connecting, disconnecting, etc.

See 1124.

model.update() now supports overwrite

You can now pass the overwrite option to a model in order to override default update semantics.

overwrite is passed in via setOptions

var q = Model.where({ _id: id }).setOptions({ overwrite: true });

This will cause the update to ignore the built-in protections in Mongoose and simply send the update as-is. This means that passing {} to update with overwrite will result in an empty document. This feature is useful when you need to override the default $set update behavior.

Model.findOne(id, function (err, doc) {
  console.log(doc); // { _id: 108, name: 'cajon' })

  Model.where({ _id : id }).setOptions({ overwrite: true }).update({ changed: true }, function (err) {
    base.findOne(function (err, doc) {
      console.log(doc); // { _id: 108, changed: true }) - the doc was overwritten

nearSphere deprecated

While nearSphere still exists in the API, it will be removed in a future release. In its place, use the near() method with the { spherical : true } option.


Model.nearSphere({ center : [10,10] });
// becomes...
Model.near({ center : [10,10], spherical : true })

Validation against nonsensical queries

The new query engine will now throw an error when nonsensical or invalid combinations are attempted. Some of these include:

  • using slice with distinct
  • using limit with distinct
  • using mod without a preceding where
  • using maxScan with count
  • and more

Important to note that these types of queries never worked before. They would either not return results that were truly representative of the query or would fail silently.

Query#center is deprecated

Please use Query#circle instead.

var area = { center: [50, 50], radius: 10, unique: true }

Query#centerSphere is deprecated

Please use Query#circle instead with the spherical option set.

var area = { center: [50, 50], radius: 10, unique: true, spherical: true }

Query#slaveOk is deprecated

Use Query#read instead with secondaryPreferred.

Promise#addBack is deprecated

Use Promise#onResolve instead.

Promise#addCallback is deprecated

Use Promise#onFulFill instead.

Promise#addErrback is deprecated

Use Promise#onReject instead.

Promise#complete is deprecated

Use Promise#fulfill instead.

document.remove callback arguments

Callbacks passed to document.remove() now receive the document which was removed.

model.findOne(function (err, doc1) {
  doc1.remove(function (err, doc2) {
    // doc2 == doc1


MongooseBuffer has a new subtype() method which sets it's subtype option and marks the buffer modified if necessary.

var bson = require('bson');

See this page for more detail.

awaitdata support

Query#tailable() now supports the awaitdata option.

query.tailable({ awaitdata: true })

Query#update and Query#remove only executes with callback or explicit true

Previously, update() and remove() would execute an unsafe update/delete if no callback was passed. This has been changed in this release. You now must either pass a callback or explicitly tell Mongoose to execute the query in an unsafe manner. This can be done by passing true for the callback value to the query.

query.update({...},{...}, true);
Something went wrong with that request. Please try again.