{"record":{"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":"exception","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/dce7939f86fb283e167ad709955abedb7bf23124/src/bulk/common.ts#L1039-L1075","documentation":"Thrown as MongoInvalidArgumentError by BulkOperationBase.find() (src/bulk/common.ts:1057) when no selector (filter) is supplied. find(selector) stores the selector for subsequent update/delete/replace calls; a missing selector means there is no query to scope those operations to, so the driver refuses to build them.","triggerScenarios":"Calling bulk.find() with no argument, or bulk.find(null)/bulk.find(undefined) when a variable holding the filter was not assigned. Guarded by `if (!selector)` at src/bulk/common.ts:1056.","commonSituations":"Filter variable computed conditionally and left undefined, a destructuring bug that drops the filter, or an assumption that find() with no args matches all documents (it does not — pass {} explicitly for that).","solutions":["Pass an explicit filter document: bulk.find({ userId: 42 }).","If you genuinely mean 'all documents', pass an empty query: bulk.find({}).updateOne({...}) (and reconsider whether a targeted filter is safer).","Initialize the filter variable with a default of {} so it is never undefined."],"exampleFix":"// before\nbulk.find().updateOne({ $set: { archived: true } });\n\n// after\nbulk.find({}).updateOne({ $set: { archived: true } });","handlingStrategy":"validation","validationCode":"function ensureSelector(selector: unknown) {\n  if (!selector || typeof selector !== 'object' || Array.isArray(selector)) {\n    throw new Error('bulk.find() requires a non-null filter document; use {} for all docs');\n  }\n  return selector as Record<string, unknown>;\n}\n\nbulk.find(ensureSelector(maybeFilter)).updateOne({ $set: { a: 1 } });","typeGuard":"function isFilterDocument(sel: unknown): sel is Record<string, unknown> {\n  return sel != null && typeof sel === 'object' && !Array.isArray(sel);\n}","tryCatchPattern":"try {\n  bulk.find(filter).updateOne({ $set: { a: 1 } });\n} catch (e) {\n  if (e instanceof MongoInvalidArgumentError && /must specify a selector/.test(e.message)) {\n    bulk.find({}).updateOne({ $set: { a: 1 } });\n  } else throw e;\n}","preventionTips":["Default filter variables to {} so they are never undefined.","Pass {} explicitly when you mean 'all documents'.","Type the filter parameter as Record<string, unknown>, not as optional.","Reject null/undefined filters in input validation at API boundaries."],"tags":["bulk-write","find","selector","validation"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}