{"record":{"id":"d5746d183a20d3e0","repo":"Automattic/mongoose","slug":"cannot-call-create-with-a-session-and-multiple","errorCode":null,"errorMessage":"Cannot call `create()` with a session and multiple documents unless `ordered: true` is set","messagePattern":"Cannot call `create\\(\\)` with a session and multiple documents unless `ordered: true` is set","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/model.js","lineNumber":2746,"sourceCode":"        !this.schema.path('session')) {\n      // Probably means the user is running into the common mistake of trying\n      // to use a spread to specify options, see gh-7535\n      utils.warn('WARNING: to pass a `session` to `Model.create()` in ' +\n        'Mongoose, you **must** pass an array as the first argument. See: ' +\n        'https://mongoosejs.com/docs/api/model.html#Model.create()');\n    }\n  }\n\n  if (args.length === 0) {\n    return Array.isArray(doc) ? [] : null;\n  }\n  let res = [];\n  const immediateError = typeof options.aggregateErrors === 'boolean' ? !options.aggregateErrors : true;\n\n  delete options.aggregateErrors; // dont pass on the option to \"$save\"\n\n  if (options.session && !options.ordered && args.length > 1) {\n    throw new MongooseError('Cannot call `create()` with a session and multiple documents unless `ordered: true` is set');\n  }\n\n  if (!Array.isArray(doc) && args.length === 1) {\n    let toSave = doc;\n\n    const Model = this.discriminators && doc[discriminatorKey] != null ?\n      this.discriminators[doc[discriminatorKey]] || getDiscriminatorByValue(this.discriminators, doc[discriminatorKey]) :\n      this;\n    if (Model == null) {\n      throw new MongooseError(`Discriminator \"${doc[discriminatorKey]}\" not ` +\n      `found for model \"${this.modelName}\"`);\n    }\n\n    if (!(toSave instanceof Model)) {\n      toSave = new Model(toSave);\n    }\n\n    await toSave.$save(options);","sourceCodeStart":2728,"sourceCodeEnd":2764,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/model.js#L2728-L2764","documentation":"Model.create() saves multiple documents in parallel with Promise.all by default. When a session is supplied, parallel saves can interleave on the same session (typically inside a transaction), so Mongoose requires ordered: true, which switches to a serial for-loop over $save() calls. If options.session is set, options.ordered is falsy, and more than one document is being created, this MongooseError is thrown (as a rejected promise, since create() is async).","triggerScenarios":"await Model.create([d1, d2], { session }) inside a transaction (or with any session) without ordered: true. Also Model.create(d1, d2, { session }) spread form with a session in options.","commonSituations":"Wrapping multi-document creation in withTransaction(); adding { session } to existing bulk create calls during a refactor to transactions; passing a session from caller middleware to a generic create helper.","solutions":["Add ordered: true: await Model.create([d1, d2], { session, ordered: true })","Or create documents one at a time in the session: for (const d of docs) await Model.create(d, { session })","Or use insertMany(docs, { session }) when middleware/hooks are not needed (insertMany is ordered by default)","Only pass a session when one is actually required; plain create() calls without a session are unaffected"],"exampleFix":"// before\nawait session.withTransaction(async () => {\n  await User.create([u1, u2], { session }); // throws\n});\n\n// after\nawait session.withTransaction(async () => {\n  await User.create([u1, u2], { session, ordered: true });\n});","handlingStrategy":"validation","validationCode":"function createWithSession(Model, docs, session) {\n  const opts = { session, ordered: true };\n  if (opts.session && !opts.ordered && docs.length > 1) {\n    throw new Error('pass ordered: true when using a session with multiple docs');\n  }\n  return Model.create(docs, opts);\n}","typeGuard":null,"tryCatchPattern":"try {\n  await Model.create(docs, { session, ordered: true });\n} catch (err) {\n  if (/unless `ordered: true` is set/.test(err.message)) {\n    await Model.create(docs, { session, ordered: true }); // retry with ordered\n  } else throw err;\n}","preventionTips":["Treat { session } as implying ordered: true in your create helpers","Keep transaction code paths in one wrapper so session handling is uniform","Prefer insertMany(docs, { session }) when hooks are unnecessary"],"tags":["mongoose","transactions","session","ordered","create","mongodb"],"backgroundTag":"mongodb-session-transaction-constraints","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}