{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/operations/find_and_modify.ts#L256-L292","documentation":"FindOneAndUpdateOperation's constructor (src/operations/find_and_modify.ts:274) requires the `update` argument to be a non-null object. The update document (or aggregation pipeline) must be a plain object or array; null/undefined/string/number all throw a MongoInvalidArgumentError client-side. A subsequent check enforces that the update contains atomic operators or is a pipeline.","triggerScenarios":"Calling collection.findOneAndUpdate(filter, null), collection.findOneAndUpdate(filter, undefined), or passing a primitive/string as the update.","commonSituations":"Omitting the update argument, passing a field value instead of an update document, or destructuring undefined from a request payload.","solutions":["Pass an update document with atomic operators: collection.findOneAndUpdate(filter, { $set: { field: value } }).","Or pass an aggregation pipeline array for expressive updates.","Ensure the update variable is a defined object/array before the call."],"exampleFix":"// before\nawait collection.findOneAndUpdate({ _id }, 'new value');\n\n// after\nawait collection.findOneAndUpdate({ _id }, { $set: { value: 'new value' } });","handlingStrategy":"validation","validationCode":"function assertUpdate(update: unknown): asserts update is Record<string, unknown> {\n  if (update == null || typeof update !== 'object') {\n    throw new TypeError('findOneAndUpdate: update must be an object or pipeline');\n  }\n}","typeGuard":"const isValidUpdate = (u: unknown): u is Record<string, unknown> | unknown[] =>\n  u != null && typeof u === 'object';","tryCatchPattern":null,"preventionTips":["Pass an update document with atomic operators or an aggregation pipeline.","Ensure the update variable is defined and an object before calling.","Use TypeScript Document/Array types on the update parameter."],"tags":["argument-validation","find-one-and-modify","update","client-side"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}