{"record":{"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/dce7939f86fb283e167ad709955abedb7bf23124/src/gridfs/index.ts#L219-L255","documentation":"GridFSBucket.rename (src/gridfs/index.ts:232) updates the filename on the file document. If the updateOne matches zero documents (matchedCount === 0), it throws MongoRuntimeError 'File with id not found'. The id must correspond to an existing file in this bucket's files collection.","triggerScenarios":"Calling bucket.rename(id, newFilename) for a non-existent or already-deleted id. Passing a string instead of an ObjectId, or an id from a different bucket.","commonSituations":"Renaming after a concurrent delete. Mismatched bucket name between upload and rename. Stale cached id.","solutions":["Verify the file exists before renaming: const file = await bucket.find({ _id: id }).next(); if (file) await bucket.rename(id, newName).","Confirm the bucket instance (bucketName option) matches the one used at upload.","Ensure id is the ObjectId returned from the upload stream."],"exampleFix":"// before\nawait bucket.rename(id, 'new-name.log');\n// after\nconst file = await bucket.find({ _id: id }).next();\nif (file) await bucket.rename(id, 'new-name.log');","handlingStrategy":"try-catch","validationCode":"async function renameIfExists(bucket, id, newName) {\n  const file = await bucket.find({ _id: id }).next();\n  if (!file) return false;\n  await bucket.rename(id, newName);\n  return true;\n}","typeGuard":"function isObjectId(v: unknown): v is ObjectId {\n  return v != null && typeof v === 'object' && '_bsontype' in v && (v as any)._bsontype === 'ObjectId';\n}","tryCatchPattern":"try {\n  await bucket.rename(id, newName);\n} catch (e) {\n  if (e instanceof MongoRuntimeError && /not found/.test(e.message)) {\n    // handle missing file\n  } else throw e;\n}","preventionTips":["Check existence before renaming.","Ensure the bucket instance matches the upload bucket.","Use the ObjectId from the upload stream's 'id' property."],"tags":["gridfs","not-found","validation"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}