mongodb/node-mongodb-native · error · MongoInvalidArgumentError

Query document must be specified for query

Error message

Query document must be specified for query

What it means

Thrown by the OpMsgRequest constructor (commands.ts:537) as a MongoInvalidArgumentError when the command document is null or undefined. OP_MSG is the modern wire protocol message; it requires a command body to serialize. Like errors 95/96 this guards an internal invariant - the public operation layer always supplies a command document.

Source

Thrown at src/cmap/commands.ts:537

/** @internal */
export class OpMsgRequest {
  requestId: number;
  serializeFunctions: boolean;
  ignoreUndefined: boolean;
  checkKeys: boolean;
  maxBsonSize: number;
  checksumPresent: boolean;
  moreToCome: boolean;
  exhaustAllowed: boolean;
  databaseName: string;
  command: Document;
  options: OpQueryOptions;

  constructor(databaseName: string, command: Document, options: OpQueryOptions) {
    // Basic options needed to be passed in
    if (command == null)
      throw new MongoInvalidArgumentError('Query document must be specified for query');

    // Basic optionsa
    this.databaseName = databaseName;
    this.command = command;
    this.command.$db = databaseName;

    // Ensure empty options
    this.options = options ?? {};

    // Additional options
    this.requestId = options.requestId ? options.requestId : OpMsgRequest.getRequestId();

    // Serialization option
    this.serializeFunctions =
      typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
    this.ignoreUndefined =
      typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : false;
    this.checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Report as a driver bug with the operation and driver version
  2. Use the public Collection/Db APIs; do not construct OpMsgRequest manually
  3. If writing a custom Operation, ensure buildCommand() always returns a Document
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await client.connect();
} catch (e) {
  if (e instanceof MongoInvalidArgumentError && /Query document must be specified/i.test(e.message)) {
    // internal bug - report to driver maintainers
  }
  throw e;
}

Prevention

When it happens

Trigger: Internal code calling new OpMsgRequest(db, null, options) or new OpMsgRequest(db, undefined, options). Reachable only via driver internals, custom wire-protocol code, or a defect where a command builder returned null.

Common situations: Driver bug in a custom operation whose buildCommand() returned undefined; extension code that bypassed the standard Operation infrastructure.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04). Data as JSON: /data/errors/ce20f7a87ba8c2cd.json. Report an issue: GitHub.