{"id":"d739afee1ad0cd2c","repo":"mongodb/node-mongodb-native","slug":"file-with-id-id-not-found","errorCode":null,"errorMessage":"File with id ${id} not found","messagePattern":"File with id (.+?) not found","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/gridfs/index.ts","lineNumber":237,"sourceCode":"      this.s._filesCollection,\n      this.s.options.readPreference,\n      { filename },\n      { timeoutMS: this.s.options.timeoutMS, ...options, sort, skip }\n    );\n  }\n\n  /**\n   * Renames the file with the given _id to the given string\n   *\n   * @param id - the id of the file to rename\n   * @param filename - new name for the file\n   */\n  async rename(id: ObjectId, filename: string, options?: { timeoutMS: number }): Promise<void> {\n    const filter = { _id: id };\n    const update = { $set: { filename } };\n    const { matchedCount } = await this.s._filesCollection.updateOne(filter, update, options);\n    if (matchedCount === 0) {\n      throw new MongoRuntimeError(`File with id ${id} not found`);\n    }\n  }\n\n  /** Removes this bucket's files collection, followed by its chunks collection. */\n  async drop(options?: { timeoutMS: number }): Promise<void> {\n    const { timeoutMS } = resolveOptions(this.s.db, options);\n    let timeoutContext: CSOTTimeoutContext | undefined = undefined;\n\n    if (timeoutMS) {\n      timeoutContext = new CSOTTimeoutContext({\n        timeoutMS,\n        serverSelectionTimeoutMS: this.s.db.client.s.options.serverSelectionTimeoutMS\n      });\n    }\n\n    if (timeoutContext) {\n      await this.s._filesCollection.drop({ timeoutMS: timeoutContext.remainingTimeMS });\n      const remainingTimeMS = timeoutContext.getRemainingTimeMSOrThrow(","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/gridfs/index.ts#L219-L255","documentation":"Thrown by GridFSBucket.rename() when updateOne against the files collection matched zero documents (matchedCount === 0). It is a MongoRuntimeError indicating the target file does not exist, so its filename cannot be changed. No documents are modified when this fires. Distinct from the delete() variant (error 260) only by message and method.","triggerScenarios":"Calling bucket.rename(id, newName) with an id not present in the bucket's files collection. Reusing an id after deletion, pointing at the wrong bucket/database, or passing a non-ObjectId value.","commonSituations":"Renaming already-deleted files; multi-tenant systems where the id came from another tenant's bucket; passing the filename as the first arg instead of the _id.","solutions":["Look up the file first: const f = await bucket.find({ _id: id }, { limit: 1 }).next(); if (f) await bucket.rename(id, newName);","Make sure id is an ObjectId and that the GridFSBucket uses the matching db/bucketName.","If rename is best-effort, catch MongoRuntimeError and swallow the missing-file case."],"exampleFix":"// before\nawait bucket.rename(id, 'renamed.bin');\n\n// after\nconst file = await bucket.find({ _id: id }, { limit: 1 }).next();\nif (file) await bucket.rename(id, 'renamed.bin');","handlingStrategy":"validation","validationCode":"async function safeRename(bucket, id, name) {\n  const exists = await bucket.find({ _id: id }, { limit: 1 }).hasNext();\n  if (exists) await bucket.rename(id, name);\n}","typeGuard":"function isObjectId(v): v is ObjectId { return v instanceof ObjectId; }","tryCatchPattern":"try {\n  await bucket.rename(id, name);\n} catch (e) {\n  if (e instanceof MongoRuntimeError && /not found/.test(e.message)) return;\n  throw e;\n}","preventionTips":["Look up file before rename","Keep id as ObjectId end-to-end","Catch and ignore benign missing-file renames in idempotent jobs"],"tags":["gridfs","file-not-found","rename"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}