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
- Drop `new` when compiling: const Test = conn.model('Test', schema)
- 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
- conn.model()/mongoose.model() is a factory — never precede it with new
- Reserve new for documents: const doc = new Model({...})
- Lint for `new conn.model` / `new mongoose.model` patterns in review
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
- The 2nd parameter to `mongoose.model()` should be a schema o
- First parameter to `deleteModel()` must be a string or regex
- 2nd argument to `Model` constructor must be a POJO or string
- First argument to `Model` constructor must be an object, **n
- Invalid arguments
AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21).
Data as JSON: /api/errors/5c79ebba993f855d.
Report an issue: GitHub.