{"record":{"id":"6bd8a7e9041e9e55","repo":"Automattic/mongoose","slug":"invalid-arguments","errorCode":null,"errorMessage":"Invalid arguments","messagePattern":"Invalid arguments","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"warning","filePath":"lib/aggregate.js","lineNumber":780,"sourceCode":" * @api public\n */\n\nAggregate.prototype.redact = function(expression, thenExpr, elseExpr) {\n  if (arguments.length === 3) {\n    if ((typeof thenExpr === 'string' && !validRedactStringValues.has(thenExpr)) ||\n      (typeof elseExpr === 'string' && !validRedactStringValues.has(elseExpr))) {\n      throw new MongooseError('If thenExpr or elseExpr is string, it must be either $$DESCEND, $$PRUNE or $$KEEP');\n    }\n\n    expression = {\n      $cond: {\n        if: expression,\n        then: thenExpr,\n        else: elseExpr\n      }\n    };\n  } else if (arguments.length !== 1) {\n    throw new TypeError('Invalid arguments');\n  }\n\n  return this.append({ $redact: expression });\n};\n\n/**\n * Execute the aggregation with explain\n *\n * #### Example:\n *\n *     Model.aggregate(..).explain()\n *\n * @param {'queryPlanner'|'executionStats'|'allPlansExecution'} [verbosity]\n * @return {Promise}\n */\n\nAggregate.prototype.explain = async function explain(verbosity) {\n  if (typeof verbosity === 'function' || typeof arguments[1] === 'function') {","sourceCodeStart":762,"sourceCodeEnd":798,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/aggregate.js#L762-L798","documentation":"Model.create() accepts either a spread of documents or a single array plus options. There is no overloaded signature like create(doc, { session }) — the second argument would be interpreted as another document. To catch this classic mistake (gh-7535), Mongoose heuristically detects exactly two args where the last one is a ClientSession and warns that the session will not be applied, pointing you to the array form create([docs], { session }). The heuristic skips models that define their own 'session' path.","triggerScenarios":"await Model.create({ name: 'x' }, { session }) — the options object holds a ClientSession; also Model.create(docA, { session: session, validateBeforeSave: false })-style spread attempts; anything where args.length === 2, the first arg is a non-null object without .session, and last.session is a ClientSession instance.","commonSituations":"Copy-pasting session usage from findOneAndUpdate (where an options object IS the second parameter) into create(); transaction helpers that wrap writes: sessionManager.withSession(s => Model.create(doc, { session: s })); code that worked without transactions and later gained a session argument.","solutions":["Pass an array as the first argument: await Model.create([doc], { session }).","For multiple docs: await Model.create([docA, docB], { session }).","Note that with the spread form (no array) the result is a single doc instead of an array — the array form also makes return shapes consistent inside transactions.","Keep transaction logic in one helper so every create uses the array form by construction."],"exampleFix":"// before\nawait Model.create({ name: 'x' }, { session });\n\n// after\nawait Model.create([{ name: 'x' }], { session });","handlingStrategy":"validation","validationCode":"async function createWithSession(Model, docs, session) {\n  const arr = Array.isArray(docs) ? docs : [docs];\n  return Model.create(arr, { session }); // array first, options second — always\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Inside transactions, always use the array form Model.create([...], { session }).","Centralize session handling in a withTransaction helper that enforces correct argument shapes.","Remember: with a spread (non-array) first arg the result is a single doc; the array form returns an array — code accordingly."],"tags":["mongoose","transactions","session","model-create","api-misuse"],"backgroundTag":"wrong-function-arguments","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}