{"id":"ccb699ac76c460a1","repo":"mongodb/node-mongodb-native","slug":"dynamic-server-write-error-propagated-via-mongos","errorCode":null,"errorMessage":"<dynamic: server write error propagated via MongoServerError(res.writeErrors[0])>","messagePattern":"<dynamic: server write error propagated via MongoServerError\\(res\\.writeErrors\\[0\\]\\)>","errorType":"exception","errorClass":"MongoServerError","httpStatus":null,"severity":"error","filePath":"src/operations/delete.ts","lineNumber":117,"sourceCode":"    return command;\n  }\n}\n\nexport class DeleteOneOperation extends DeleteOperation {\n  constructor(ns: MongoDBCollectionNamespace, filter: Document, options: DeleteOptions) {\n    super(ns, [makeDeleteStatement(filter, { ...options, limit: 1 })], options);\n  }\n\n  override handleOk(\n    response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>\n  ): DeleteResult {\n    const res = super.handleOk(response);\n\n    // @ts-expect-error Explain commands have broken TS\n    if (this.explain) return res;\n\n    if (res.code) throw new MongoServerError(res);\n    if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]);\n\n    return {\n      acknowledged: this.writeConcern?.w !== 0,\n      deletedCount: res.n\n    };\n  }\n}\nexport class DeleteManyOperation extends DeleteOperation {\n  constructor(ns: MongoDBCollectionNamespace, filter: Document, options: DeleteOptions) {\n    super(ns, [makeDeleteStatement(filter, options)], options);\n  }\n\n  override handleOk(\n    response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>\n  ): DeleteResult {\n    const res = super.handleOk(response);\n\n    // @ts-expect-error Explain commands have broken TS","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/operations/delete.ts#L99-L135","documentation":"In DeleteOneOperation.handleOk (src/operations/delete.ts:117), after a successful round-trip, the driver checks for per-document write errors in the response. If `writeErrors` is present, the first entry is wrapped in a MongoServerError and thrown. This is how the driver surfaces individual document-level write failures such as duplicate-key or schema-validation errors.","triggerScenarios":"collection.deleteOne(filter) where the server applies the delete but reports a write error — most commonly a document-validation failure or a write concern error tied to the single document.","commonSituations":"Collection has a JSON schema validator that rejects the operation context, or a write concern error is reported per-document. Because deleteOne affects a single document, the writeErrors array typically contains exactly one entry.","solutions":["Read err.writeErrors (or err.errmsg) to see the per-document failure detail; the message and code come from the server.","If err.code is 121 (DocumentValidationFailure), review the collection's $jsonSchema validator and the document being deleted.","For transient failures, check for the RetryableError label before retrying.","If the error is not actionable, surface it to the user with the original server message."],"exampleFix":"// before\nawait collection.deleteOne({ sku: 'X-1' });\n\n// after\ntry {\n  await collection.deleteOne({ sku: 'X-1' });\n} catch (err) {\n  if (err instanceof MongoServerError && err.writeErrors) {\n    const [first] = err.writeErrors;\n    console.error(`delete write error ${first.code}: ${first.errmsg}`);\n  }\n  throw err;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await collection.deleteOne(filter);\n} catch (err) {\n  if (err instanceof MongoServerError && err.writeErrors) {\n    const first = err.writeErrors[0];\n    // handle first.code (e.g. 121 = validation)\n  } else throw err;\n}","preventionTips":["Review collection $jsonSchema validators before deletes if validation is enforced.","For deleteOne, expect at most one writeError entry.","Log err.writeErrors to capture per-document detail."],"tags":["server-error","delete","write-error","validation"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}