{"record":{"id":"5c79ebba993f855d","repo":"Automattic/mongoose","slug":"connection-model-should-not-be-run-with-new","errorCode":null,"errorMessage":"`connection.model()` should not be run with `new`. If you are doing `new db.model(foo)(bar)`, use `db.model(foo)(bar)` instead","messagePattern":"`connection\\.model\\(\\)` should not be run with `new`\\. If you are doing `new db\\.model\\(foo\\)\\(bar\\)`, use `db\\.model\\(foo\\)\\(bar\\)` instead","errorType":"validation","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/connection.js","lineNumber":1438,"sourceCode":" *\n *     // or\n *\n *     const collectionName = 'actor'\n *     const M = conn.model('Actor', schema, collectionName)\n *\n * @param {string|Function} name the model name or class extending Model\n * @param {Schema} [schema] a schema. necessary when defining a model\n * @param {string} [collection] name of mongodb collection (optional) if not given it will be induced from model name\n * @param {object} [options]\n * @param {boolean} [options.overwriteModels=false] If true, overwrite existing models with the same name to avoid `OverwriteModelError`\n * @see Mongoose#model https://mongoosejs.com/docs/api/mongoose.html#Mongoose.prototype.model()\n * @return {Model} The compiled model\n * @api public\n */\n\nConnection.prototype.model = function model(name, schema, collection, options) {\n  if (!(this instanceof Connection)) {\n    throw new MongooseError('`connection.model()` should not be run with ' +\n      '`new`. If you are doing `new db.model(foo)(bar)`, use ' +\n      '`db.model(foo)(bar)` instead');\n  }\n\n  let fn;\n  if (typeof name === 'function') {\n    fn = name;\n    name = fn.name;\n  }\n\n  // collection name discovery\n  if (typeof schema === 'string') {\n    collection = schema;\n    schema = false;\n  }\n\n  if (utils.isObject(schema)) {\n    if (!schema.instanceOfSchema) {","sourceCodeStart":1420,"sourceCodeEnd":1456,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/connection.js#L1420-L1456","documentation":"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`.","triggerScenarios":"const Test = new conn.model('Test', schema); or new mongoose.connection.model('Test')(doc) — muscle memory from `new Model(doc)` applied to model compilation.","commonSituations":"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.","solutions":["Drop `new` when compiling: const Test = conn.model('Test', schema)","Instantiate documents separately: const doc = new Test({ name: 'test' })"],"exampleFix":"// before\nconst Test = new conn.model('Test', testSchema);\n\n// after\nconst Test = conn.model('Test', testSchema);\nconst doc = new Test({ name: 'test' });","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["model","constructor-misuse","api-misuse"],"backgroundTag":"constructor-misuse","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}