{"record":{"id":"55e73193b99c4ff3","repo":"Automattic/mongoose","slug":"cannot-overwrite-options-in-pre-save-hook-on-bu","errorCode":null,"errorMessage":"Cannot overwrite options in pre(\"save\") hook on bulkSave()","messagePattern":"Cannot overwrite options in pre\\(\"save\"\\) hook on bulkSave\\(\\)","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/model.js","lineNumber":3682,"sourceCode":"\n    if (documentError == null) {\n      successfulDocuments.push(document);\n    }\n  }\n  await Promise.all(successfulDocuments.map(document => handleSuccessfulWrite(document, options)));\n\n  if (bulkWriteError != null) {\n    throw bulkWriteError;\n  }\n\n  return bulkWriteResult;\n};\n\nasync function buildPreSavePromise(document, options) {\n  const preFilter = buildMiddlewareFilter(options, 'pre');\n  const [newOptions] = await document.schema.s.hooks.execPre('save', document, [options], { filter: preFilter });\n  if (newOptions !== options) {\n    throw new MongooseError('Cannot overwrite options in pre(\"save\") hook on bulkSave()');\n  }\n}\n\nasync function handleSuccessfulWrite(document, options) {\n  const wasNew = document.$isNew;\n  if (wasNew) {\n    _setIsNew(document, false);\n  }\n\n  document.$__reset();\n  // version key gets initialized to 0 when inserting a new document so avoid incrementing version key here\n  if (!wasNew) {\n    document._applyVersionIncrement();\n  }\n  const postFilter = buildMiddlewareFilter(options, 'post');\n  return document.schema.s.hooks.execPost('save', document, [document], { filter: postFilter });\n}\n","sourceCodeStart":3664,"sourceCodeEnd":3700,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/model.js#L3664-L3700","documentation":"Error \"Cannot overwrite options in pre(\"save\") hook on bulkSave()\" thrown in Automattic/mongoose.","triggerScenarios":"Thrown at lib/model.js:3682 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}