{"id":"3f944425ec7bdf72","repo":"mongodb/node-mongodb-native","slug":"bulk-find-operation-must-specify-a-selector","errorCode":null,"errorMessage":"Bulk find operation must specify a selector","messagePattern":"Bulk find operation must specify a selector","errorType":"validation","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/bulk/common.ts","lineNumber":1057,"sourceCode":"   *\n   * // Add a multi deletion\n   * bulkOp.find({ h: 8 }).delete();\n   *\n   * // Add a replaceOne\n   * bulkOp.find({ i: 9 }).replaceOne({writeConcern: { j: 10 }});\n   *\n   * // Update using a pipeline\n   * bulk.find({ k: 11, y: { $exists: true }, z: { $exists: true } }).updateOne([\n   *   { $set: { total: { $sum: [ '$y', '$z' ] } } }\n   * ]);\n   *\n   * // All of the ops will now be executed\n   * await bulkOp.execute();\n   * ```\n   */\n  find(selector: Document): FindOperators {\n    if (!selector) {\n      throw new MongoInvalidArgumentError('Bulk find operation must specify a selector');\n    }\n\n    // Save a current selector\n    this.s.currentOp = {\n      selector: selector\n    };\n\n    return new FindOperators(this);\n  }\n\n  /** Specifies a raw operation to perform in the bulk write. */\n  raw(op: AnyBulkWriteOperation): this {\n    if (op == null || typeof op !== 'object') {\n      throw new MongoInvalidArgumentError('Operation must be an object with an operation key');\n    }\n    if ('insertOne' in op) {\n      const forceServerObjectId = this.shouldForceServerObjectId();\n      const document =","sourceCodeStart":1039,"sourceCodeEnd":1075,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/bulk/common.ts#L1039-L1075","documentation":"Thrown by BulkOperationBase.find(selector) when the selector argument is falsy (undefined, null, 0, '', false). Every bulk operation that targets existing documents needs a query filter; an empty/missing filter would implicitly match all docs and the driver requires an explicit selector to prevent accidental full-collection operations.","triggerScenarios":"Calling bulk.find(undefined) or bulk.find(null). Calling bulk.find(someVar) where someVar was never assigned. Calling bulk.find() with no argument.","commonSituations":"Dynamic filter building where the filter object is conditionally constructed and ends up undefined; refactoring that drops the selector; passing a destructured value that was undefined.","solutions":["Pass an explicit selector, even an empty object {} matches all documents intentionally: bulk.find({}).","Guard dynamic selectors: if (!selector) throw new Error('selector required') before calling find.","Verify the variable feeding find() is actually defined at runtime."],"exampleFix":"// before\nconst filter = maybeGetFilter(); // returns undefined\nbulk.find(filter).updateOne({ $set: { a: 1 } });\n\n// after\nconst filter = maybeGetFilter() ?? {};\nbulk.find(filter).updateOne({ $set: { a: 1 } });","handlingStrategy":"validation","validationCode":"function findSafe(bulk, selector) {\n  if (selector == null) {\n    throw new TypeError('bulk.find() requires a selector; pass {} to match all');\n  }\n  return bulk.find(selector);\n}","typeGuard":"function isNonEmptySelector(sel) {\n  return sel != null && typeof sel === 'object';\n}","tryCatchPattern":"try {\n  bulk.find(selector).updateOne({ $set: { a: 1 } });\n} catch (e) {\n  if (e instanceof MongoInvalidArgumentError && /selector/.test(e.message)) {\n    selector = {};\n    bulk.find(selector).updateOne({ $set: { a: 1 } });\n  }\n}","preventionTips":["Default dynamic selectors with ?? {}.","Static-analyze call sites to ensure find() always receives an argument."],"tags":["bulk-write","filter","validation","argument-error"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}