{"record":{"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/dce7939f86fb283e167ad709955abedb7bf23124/src/operations/find_and_modify.ts#L188-L224","documentation":"Thrown as a MongoInvalidArgumentError by the FindOneAndDeleteOperation constructor when the filter argument is null, undefined, or not an object type. findOneAndDelete requires a plain query document to identify the single document to remove. The check at find_and_modify.ts:205 is `filter == null || typeof filter !== 'object'`.","triggerScenarios":"Calling collection.findOneAndDelete(undefined), collection.findOneAndDelete(null), or with a primitive filter value. Note arrays pass the typeof check but are generally not valid query documents semantically.","commonSituations":"Omitting the filter argument; passing a string id; passing a variable that was never assigned from untyped input.","solutions":["Pass a plain object filter, e.g. collection.findOneAndDelete({ _id: new ObjectId(id) }).","To match any document use an empty object: collection.findOneAndDelete({}).","Validate that the filter is a non-null object before calling the API."],"exampleFix":"// before\nawait collection.findOneAndDelete(); // missing filter -> error\n\n// after\nawait collection.findOneAndDelete({ _id: new ObjectId(id) });","handlingStrategy":"type-guard","validationCode":"function isQueryObject(v: unknown): v is Record<string, unknown> {\n  return typeof v === 'object' && v !== null && !Array.isArray(v);\n}\nif (!isQueryObject(filter)) {\n  throw new TypeError('findOneAndDelete filter must be a plain object');\n}\nawait collection.findOneAndDelete(filter);","typeGuard":"function isNonNullObject(v: unknown): v is Record<string, unknown> {\n  return typeof v === 'object' && v !== null;\n}","tryCatchPattern":null,"preventionTips":["Always supply a filter object to findOneAndDelete (use {} to match any).","Validate dynamically-built filters are non-null objects."],"tags":["validation","find-one-and-delete","filter","argument"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}