mongodb/node-mongodb-native · error · MongoRuntimeError

File with id not found

Error message

File with id ${id} not found

What it means

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.

Solutions

  1. Verify the file exists before renaming: const file = await bucket.find({ _id: id }).next(); if (file) await bucket.rename(id, newName).
  2. Confirm the bucket instance (bucketName option) matches the one used at upload.
  3. Ensure id is the ObjectId returned from the upload stream.

Example fix

// before
await bucket.rename(id, 'new-name.log');
// after
const file = await bucket.find({ _id: id }).next();
if (file) await bucket.rename(id, 'new-name.log');
Defensive patterns

Strategy: try-catch

Validate before calling

async function renameIfExists(bucket, id, newName) {
  const file = await bucket.find({ _id: id }).next();
  if (!file) return false;
  await bucket.rename(id, newName);
  return true;
}

Type guard

function isObjectId(v: unknown): v is ObjectId {
  return v != null && typeof v === 'object' && '_bsontype' in v && (v as any)._bsontype === 'ObjectId';
}

Try / catch

try {
  await bucket.rename(id, newName);
} catch (e) {
  if (e instanceof MongoRuntimeError && /not found/.test(e.message)) {
    // handle missing file
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: Renaming after a concurrent delete. Mismatched bucket name between upload and rename. Stale cached id.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11). Data as JSON: /api/errors/d739afee1ad0cd2c. Report an issue: GitHub.

Appendix: source

Thrown at src/gridfs/index.ts:237

      this.s._filesCollection,
      this.s.options.readPreference,
      { filename },
      { timeoutMS: this.s.options.timeoutMS, ...options, sort, skip }
    );
  }

  /**
   * Renames the file with the given _id to the given string
   *
   * @param id - the id of the file to rename
   * @param filename - new name for the file
   */
  async rename(id: ObjectId, filename: string, options?: { timeoutMS: number }): Promise<void> {
    const filter = { _id: id };
    const update = { $set: { filename } };
    const { matchedCount } = await this.s._filesCollection.updateOne(filter, update, options);
    if (matchedCount === 0) {
      throw new MongoRuntimeError(`File with id ${id} not found`);
    }
  }

  /** Removes this bucket's files collection, followed by its chunks collection. */
  async drop(options?: { timeoutMS: number }): Promise<void> {
    const { timeoutMS } = resolveOptions(this.s.db, options);
    let timeoutContext: CSOTTimeoutContext | undefined = undefined;

    if (timeoutMS) {
      timeoutContext = new CSOTTimeoutContext({
        timeoutMS,
        serverSelectionTimeoutMS: this.s.db.client.s.options.serverSelectionTimeoutMS
      });
    }

    if (timeoutContext) {
      await this.s._filesCollection.drop({ timeoutMS: timeoutContext.remainingTimeMS });
      const remainingTimeMS = timeoutContext.getRemainingTimeMSOrThrow(

View on GitHub (pinned to dce7939f86)