{"record":{"id":"c9eb0168fd25ed18","repo":"mongodb/node-mongodb-native","slug":"argument-update-must-be-an-object","errorCode":null,"errorMessage":"Argument \"update\" must be an object","messagePattern":"Argument \"update\" must be an object","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/operations/find_and_modify.ts","lineNumber":274,"sourceCode":"}\n\n/** @internal */\nexport class FindOneAndUpdateOperation extends FindAndModifyOperation {\n  override options: FindOneAndUpdateOptions;\n\n  private update: Document;\n  constructor(\n    collection: Collection,\n    filter: Document,\n    update: Document,\n    options: FindOneAndUpdateOptions\n  ) {\n    if (filter == null || typeof filter !== 'object') {\n      throw new MongoInvalidArgumentError('Argument \"filter\" must be an object');\n    }\n\n    if (update == null || typeof update !== 'object') {\n      throw new MongoInvalidArgumentError('Argument \"update\" must be an object');\n    }\n\n    if (!hasAtomicOperators(update, options)) {\n      throw new MongoInvalidArgumentError('Update document requires atomic operators');\n    }\n\n    super(collection, filter, options);\n    this.update = update;\n    this.options = options;\n  }\n\n  override buildCommandDocument(\n    connection: Connection,\n    session?: ClientSession\n  ): Document & FindAndModifyCmdBase {\n    const document = super.buildCommandDocument(connection, session);\n    document.update = this.update;\n    configureFindAndModifyCmdBaseUpdateOpts(document, this.options);","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/operations/find_and_modify.ts#L256-L292","documentation":"Thrown as a MongoInvalidArgumentError by the FindOneAndUpdateOperation constructor when the update document is null, undefined, or not an object. The update argument must be a document with atomic operators, an update pipeline array, or similar object form. Guard at find_and_modify.ts:273.","triggerScenarios":"Calling collection.findOneAndUpdate(filter, null) or with a primitive update argument.","commonSituations":"Forgetting the update argument; passing an undefined variable; deserializing the update from input that resolved to a non-object.","solutions":["Provide a valid update document, e.g. { $set: { field: value } } or an aggregation pipeline array.","Ensure the update is a non-null object before the call.","Double-check the argument order: findOneAndUpdate(filter, update, options)."],"exampleFix":"// before\nawait collection.findOneAndUpdate({ _id: id }, undefined);\n\n// after\nawait collection.findOneAndUpdate({ _id: id }, { $inc: { count: 1 } });","handlingStrategy":"type-guard","validationCode":"function isNonNullObject(v: unknown): v is Record<string, unknown> {\n  return typeof v === 'object' && v !== null;\n}\nif (!isNonNullObject(update)) {\n  throw new TypeError('update document must be a plain object');\n}\nawait collection.findOneAndUpdate(filter, update);","typeGuard":"function isNonNullObject(v: unknown): v is Record<string, unknown> {\n  return typeof v === 'object' && v !== null;\n}","tryCatchPattern":null,"preventionTips":["Provide the update document as the second argument.","Double-check the argument order: findOneAndUpdate(filter, update, options)."],"tags":["validation","find-one-and-update","update","argument"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}