{"id":"fcbb9b96574e7a5e","repo":"mongodb/node-mongodb-native","slug":"argument-filter-must-be-an-object","errorCode":null,"errorMessage":"Argument \"filter\" must be an object","messagePattern":"Argument \"filter\" must be an object","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/operations/find_and_modify.ts","lineNumber":206,"sourceCode":"    if (options.hint) {\n      command.hint = options.hint;\n    }\n\n    return command;\n  }\n\n  override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): Document {\n    const result = super.handleOk(response);\n    return this.options.includeResultMetadata ? result : (result.value ?? null);\n  }\n}\n\n/** @internal */\nexport class FindOneAndDeleteOperation extends FindAndModifyOperation {\n  constructor(collection: Collection, filter: Document, options: FindOneAndDeleteOptions) {\n    // Basic validation\n    if (filter == null || typeof filter !== 'object') {\n      throw new MongoInvalidArgumentError('Argument \"filter\" must be an object');\n    }\n\n    super(collection, filter, options);\n  }\n\n  override buildCommandDocument(\n    connection: Connection,\n    session?: ClientSession\n  ): Document & FindAndModifyCmdBase {\n    const document = super.buildCommandDocument(connection, session);\n    document.remove = true;\n    return document;\n  }\n}\n\n/** @internal */\nexport class FindOneAndReplaceOperation extends FindAndModifyOperation {\n  private replacement: Document;","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/operations/find_and_modify.ts#L188-L224","documentation":"FindOneAndDeleteOperation's constructor (src/operations/find_and_modify.ts:206) requires the `filter` argument to be a non-null object. Passing null, undefined, a primitive, or an array throws a MongoInvalidArgumentError before any server round-trip. This validates the query that selects the document to delete.","triggerScenarios":"Calling collection.findOneAndDelete(null), collection.findOneAndDelete(undefined), collection.findOneAndDelete('id'), or collection.findOneAndDelete([cond]).","commonSituations":"Omitting the filter argument by mistake, passing a raw ID string instead of {_id: id}, or destructuring undefined from a function parameter.","solutions":["Pass a plain object filter, e.g. collection.findOneAndDelete({ _id }).","If you intend to match all, pass an empty object {} (though findOneAndDelete on {} deletes an arbitrary document).","Ensure the filter variable is defined before the call."],"exampleFix":"// before\nawait collection.findOneAndDelete(); // undefined filter\n\n// after\nawait collection.findOneAndDelete({ _id: targetId });","handlingStrategy":"validation","validationCode":"function assertFilter(filter: unknown): asserts filter is Record<string, unknown> {\n  if (filter == null || typeof filter !== 'object' || Array.isArray(filter)) {\n    throw new TypeError('findOneAndDelete: filter must be a plain object');\n  }\n}","typeGuard":"const isValidFilter = (f: unknown): f is Record<string, unknown> =>\n  f != null && typeof f === 'object' && !Array.isArray(f);","tryCatchPattern":null,"preventionTips":["Always pass an explicit filter object to findOneAndDelete.","Use TypeScript types so the compiler rejects missing arguments.","Default optional filters to {} at the call boundary, not undefined."],"tags":["argument-validation","find-one-and-modify","filter","client-side"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}