{"record":{"id":"ddbec88a31d46538","repo":"mongodb/node-mongodb-native","slug":"update-operations-require-that-all-atomic-operator","errorCode":null,"errorMessage":"Update operations require that all atomic operators have defined values, but none were provided.","messagePattern":"Update operations require that all atomic operators have defined values, but none were provided\\.","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/utils.ts","lineNumber":481,"sourceCode":"        return true;\n      }\n    }\n    return false;\n  }\n\n  const keys = Object.keys(doc);\n  // In this case we need to throw if all the atomic operators are undefined.\n  if (options?.ignoreUndefined) {\n    let allUndefined = true;\n    for (const key of keys) {\n      // eslint-disable-next-line no-restricted-syntax\n      if (doc[key] !== undefined) {\n        allUndefined = false;\n        break;\n      }\n    }\n    if (allUndefined) {\n      throw new MongoInvalidArgumentError(\n        'Update operations require that all atomic operators have defined values, but none were provided.'\n      );\n    }\n  }\n\n  return keys.length > 0 && keys[0][0] === '$';\n}\n\nexport function resolveTimeoutOptions<T extends Partial<TimeoutContextOptions>>(\n  client: MongoClient,\n  options: T\n): T &\n  Pick<\n    MongoClient['s']['options'],\n    'timeoutMS' | 'serverSelectionTimeoutMS' | 'waitQueueTimeoutMS' | 'socketTimeoutMS'\n  > {\n  const { socketTimeoutMS, serverSelectionTimeoutMS, waitQueueTimeoutMS, timeoutMS } =\n    client.s.options;","sourceCodeStart":463,"sourceCodeEnd":499,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/utils.ts#L463-L499","documentation":"Thrown by hasAtomicOperators() when the ignoreUndefined option is set and every value in the update document is undefined. This guards against updateOne/updateMany calls where all operator fields (e.g. $set values) are undefined, which would produce a no-op or malformed command. Raised as MongoInvalidArgumentError.","triggerScenarios":"Calling collection.updateOne(filter, { $set: { field: value } }, { ignoreUndefined: true }) where value resolves to undefined for all fields. Common when building update objects from optional request body fields that are all absent.","commonSituations":"API handlers that spread optional user-supplied fields into a $set object with ignoreUndefined enabled, and the incoming payload has none of the expected fields. The resulting update document has only undefined values, so the driver rejects it rather than sending an empty update.","solutions":["Filter out undefined values before constructing the update document, and ensure at least one field has a defined value.","If no fields are present to update, skip the update call entirely.","Do not enable ignoreUndefined unless you also validate that the update document is non-empty."],"exampleFix":"// before\nconst update = { $set: { name: body.name, age: body.age } }; // both undefined\ncollection.updateOne(filter, update, { ignoreUndefined: true });\n\n// after\nconst setFields = {};\nif (body.name !== undefined) setFields.name = body.name;\nif (body.age !== undefined) setFields.age = body.age;\nif (Object.keys(setFields).length) {\n  await collection.updateOne(filter, { $set: setFields }, { ignoreUndefined: true });\n}","handlingStrategy":"validation","validationCode":"function buildSet(update: Record<string, unknown>): Record<string, unknown> | null {\n  const set: Record<string, unknown> = {};\n  for (const [k, v] of Object.entries(update)) if (v !== undefined) set[k] = v;\n  return Object.keys(set).length ? set : null;\n}\nconst setFields = buildSet(req.body);\nif (setFields) await collection.updateOne(filter, { $set: setFields }, { ignoreUndefined: true });","typeGuard":"function hasDefinedValues(doc: Record<string, unknown>): boolean {\n  return Object.values(doc).some(v => v !== undefined);\n}","tryCatchPattern":"try {\n  await collection.updateOne(filter, { $set: patch }, { ignoreUndefined: true });\n} catch (e) {\n  if (e instanceof MongoInvalidArgumentError && /atomic operators have defined values/.test(e.message)) {\n    // no-op: nothing to update\n  } else throw e;\n}","preventionTips":["Strip undefined values from update objects before calling updateOne/updateMany.","Skip the update entirely when no fields have defined values.","Only enable ignoreUndefined when you also pre-validate the update document."],"tags":["update","validation","crud"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}