{"record":{"id":"cfe707cbb1e98c68","repo":"Automattic/mongoose","slug":"aggregate-not-bound-to-any-model","errorCode":null,"errorMessage":"Aggregate not bound to any Model","messagePattern":"Aggregate not bound to any Model","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"warning","filePath":"lib/aggregate.js","lineNumber":1066,"sourceCode":"    }\n  }\n\n  return this._pipeline;\n};\n\n/**\n * Executes the aggregate pipeline on the currently bound Model.\n *\n * #### Example:\n *     const result = await aggregate.exec();\n *\n * @return {Promise}\n * @api public\n */\n\nAggregate.prototype.exec = async function exec() {\n  if (!this._model && !this._connection) {\n    throw new MongooseError('Aggregate not bound to any Model');\n  }\n  if (typeof arguments[0] === 'function') {\n    throw new MongooseError('Aggregate.prototype.exec() no longer accepts a callback');\n  }\n\n  if (this._connection) {\n    if (!this._pipeline.length) {\n      throw new MongooseError('Aggregate has empty pipeline');\n    }\n\n    this._optionsForExec();\n\n    const _this = this;\n    return traceAggregate(async function maybeTracedConnectionAggregate() {\n      const cursor = await _this._connection.client.db().aggregate(_this._pipeline, _this.options);\n      return await cursor.toArray();\n    }, () => ({\n      operation: 'aggregate',","sourceCodeStart":1048,"sourceCodeEnd":1084,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/aggregate.js#L1048-L1084","documentation":"Developers frequently write new mongoose.Schema.Types.ObjectId('507f...') when they actually wanted a new ObjectId VALUE — the SchemaType constructor is for defining schema paths, not creating ids (creating one with a 24-char hex string, or with no arguments, is almost always a mistake). The constructor detects this (key is a 24-char hex string or undefined) and warns to use mongoose.Types.ObjectId instead. If you genuinely need a schema path whose key is a hex string (or you are instantiating the type with no key intentionally), pass suppressWarning: true in options.","triggerScenarios":"new mongoose.Schema.Types.ObjectId('507f1f77bcf86cd799439011') to make an id value; new mongoose.Schema.Types.ObjectId() with no key; factory helpers like id => new Schema.Types.ObjectId(id) used for seeding/default ids (the right tool there is default: () => new mongoose.Types.ObjectId()).","commonSituations":"Confusion between mongoose.Types.ObjectId (the driver ObjectId class) and mongoose.Schema.Types.ObjectId (the SchemaType) — the classic double-namespacing trap; seeding scripts; default _id factories; refactoring where a schema-path definition lost its field-name argument.","solutions":["To create an id value: new mongoose.Types.ObjectId() or new mongoose.Types.ObjectId('507f1f77bcf86cd799439011').","For default ids in a schema: { type: Schema.Types.ObjectId, default: () => new mongoose.Types.ObjectId() }.","For a real schema path, pass the field name: new Schema({ owner: Schema.Types.ObjectId }).","Only if you intentionally key a path by a 24-hex string: new Schema.Types.ObjectId('507f...', { suppressWarning: true })."],"exampleFix":"// before\nconst id = new mongoose.Schema.Types.ObjectId('507f1f77bcf86cd799439011');\n\n// after\nconst id = new mongoose.Types.ObjectId('507f1f77bcf86cd799439011');","handlingStrategy":"type-guard","validationCode":"const { Types } = require('mongoose');\nfunction makeId(hex) {\n  // always the value class, never the SchemaType\n  return hex === undefined ? new Types.ObjectId() : new Types.ObjectId(hex);\n}","typeGuard":"const isHex24 = (s) => typeof s === 'string' && /^[a-f0-9]{24}$/i.test(s);","tryCatchPattern":null,"preventionTips":["Memorize the split: mongoose.Types.ObjectId creates values; mongoose.Schema.Types.ObjectId defines schema paths.","For default ids use { default: () => new mongoose.Types.ObjectId() }.","If a SchemaType is truly needed with a hex key or no key, pass { suppressWarning: true } deliberately."],"tags":["mongoose","objectid","schema-types","api-confusion","warning"],"backgroundTag":"wrong-constructor-call","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}