Automattic/mongoose · error · MongooseError

`connection.model()` should not be run with `new`. If you ar

Error message

`connection.model()` should not be run with `new`. If you are doing `new db.model(foo)(bar)`, use `db.model(foo)(bar)` instead

What it means

connection.model() is a factory, not a constructor: it compiles and returns a Model class. Calling it with `new` binds `this` to the freshly created instance, which fails the `this instanceof Connection` guard (lib/connection.js:1441), so Mongoose throws with remediation advice. This is a strict API-misuse error — model compilation must never use `new`.

Source

Thrown at lib/connection.js:1438

 *
 *     // or
 *
 *     const collectionName = 'actor'
 *     const M = conn.model('Actor', schema, collectionName)
 *
 * @param {string|Function} name the model name or class extending Model
 * @param {Schema} [schema] a schema. necessary when defining a model
 * @param {string} [collection] name of mongodb collection (optional) if not given it will be induced from model name
 * @param {object} [options]
 * @param {boolean} [options.overwriteModels=false] If true, overwrite existing models with the same name to avoid `OverwriteModelError`
 * @see Mongoose#model https://mongoosejs.com/docs/api/mongoose.html#Mongoose.prototype.model()
 * @return {Model} The compiled model
 * @api public
 */

Connection.prototype.model = function model(name, schema, collection, options) {
  if (!(this instanceof Connection)) {
    throw new MongooseError('`connection.model()` should not be run with ' +
      '`new`. If you are doing `new db.model(foo)(bar)`, use ' +
      '`db.model(foo)(bar)` instead');
  }

  let fn;
  if (typeof name === 'function') {
    fn = name;
    name = fn.name;
  }

  // collection name discovery
  if (typeof schema === 'string') {
    collection = schema;
    schema = false;
  }

  if (utils.isObject(schema)) {
    if (!schema.instanceOfSchema) {

View on GitHub (pinned to 49cdab0136)

Solutions

  1. Drop `new` when compiling: const Test = conn.model('Test', schema)
  2. Instantiate documents separately: const doc = new Test({ name: 'test' })

Example fix

// before
const Test = new conn.model('Test', testSchema);

// after
const Test = conn.model('Test', testSchema);
const doc = new Test({ name: 'test' });
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: const Test = new conn.model('Test', schema); or new mongoose.connection.model('Test')(doc) — muscle memory from `new Model(doc)` applied to model compilation.

Common situations: Developers conflating compiling a model with instantiating a document; copying patterns from other ORMs where factories are constructors; refactors that accidentally left `new` in front of a model lookup.

Related errors


AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21). Data as JSON: /api/errors/5c79ebba993f855d. Report an issue: GitHub.