mongodb/node-mongodb-native · error · MongoInvalidArgumentError

Argument "update" must be an object

Error message

Argument "update" must be an object

What it means

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.

Solutions

  1. Provide a valid update document, e.g. { $set: { field: value } } or an aggregation pipeline array.
  2. Ensure the update is a non-null object before the call.
  3. Double-check the argument order: findOneAndUpdate(filter, update, options).

Example fix

// before
await collection.findOneAndUpdate({ _id: id }, undefined);

// after
await collection.findOneAndUpdate({ _id: id }, { $inc: { count: 1 } });
Defensive patterns

Strategy: type-guard

Validate before calling

function isNonNullObject(v: unknown): v is Record<string, unknown> {
  return typeof v === 'object' && v !== null;
}
if (!isNonNullObject(update)) {
  throw new TypeError('update document must be a plain object');
}
await collection.findOneAndUpdate(filter, update);

Type guard

function isNonNullObject(v: unknown): v is Record<string, unknown> {
  return typeof v === 'object' && v !== null;
}

Prevention

When it happens

Trigger: Calling collection.findOneAndUpdate(filter, null) or with a primitive update argument.

Common situations: Forgetting the update argument; passing an undefined variable; deserializing the update from input that resolved to a non-object.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11). Data as JSON: /api/errors/c9eb0168fd25ed18. Report an issue: GitHub.

Appendix: source

Thrown at src/operations/find_and_modify.ts:274

}

/** @internal */
export class FindOneAndUpdateOperation extends FindAndModifyOperation {
  override options: FindOneAndUpdateOptions;

  private update: Document;
  constructor(
    collection: Collection,
    filter: Document,
    update: Document,
    options: FindOneAndUpdateOptions
  ) {
    if (filter == null || typeof filter !== 'object') {
      throw new MongoInvalidArgumentError('Argument "filter" must be an object');
    }

    if (update == null || typeof update !== 'object') {
      throw new MongoInvalidArgumentError('Argument "update" must be an object');
    }

    if (!hasAtomicOperators(update, options)) {
      throw new MongoInvalidArgumentError('Update document requires atomic operators');
    }

    super(collection, filter, options);
    this.update = update;
    this.options = options;
  }

  override buildCommandDocument(
    connection: Connection,
    session?: ClientSession
  ): Document & FindAndModifyCmdBase {
    const document = super.buildCommandDocument(connection, session);
    document.update = this.update;
    configureFindAndModifyCmdBaseUpdateOpts(document, this.options);

View on GitHub (pinned to dce7939f86)