Database
MongoDB
mongorestore
BSON
Error Handling

mongorestore file X does not have .bson extension

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

mongorestore expects a dump produced by mongodump, and that means BSON collection files plus optional metadata files. If it reports that file X does not have .bson extension, it is telling you that something in the restore path is not a valid dump file for directory-based restore.

In practice, this error usually comes from one of three causes: a stray file in the dump directory, using the wrong tool for JSON data, or passing an archive file without the --archive mode.

What mongorestore Expects

When restoring from a directory, mongorestore scans for files that look like collection dumps:

  • 'collection.bson'
  • 'collection.metadata.json'

A typical dump created by mongodump looks like this:

text
1dump/
2  mydb/
3    users.bson
4    users.metadata.json
5    orders.bson
6    orders.metadata.json

This works:

bash
mongorestore dump/

If the directory also contains unrelated files such as .DS_Store, README.txt, or exported JSON files, mongorestore may complain when it encounters them.

The Most Common Real Causes

The first common cause is an extra file in the dump tree. For example, macOS often leaves .DS_Store files in folders, and backup tooling may create sidecar files that mongorestore does not know how to interpret.

The second cause is using JSON export data. If the file came from mongoexport, then mongorestore is the wrong tool entirely. mongoexport and mongoimport work with JSON or CSV. mongodump and mongorestore work with BSON dump format.

The third cause is archive mode confusion. If you created a single archive file with mongodump --archive, you must restore with mongorestore --archive, not by pointing mongorestore at the file as though it were a dump directory.

Fix 1: Clean the Dump Directory

Inspect the directory first:

bash
find dump -type f

If you see non-dump files mixed into the restore path, remove or relocate them before restoring:

bash
rm dump/.DS_Store

Then rerun:

bash
mongorestore dump/

This is the simplest fix when the dump itself is valid and only the directory contents are noisy.

Fix 2: Use the Right Tool for JSON

If the file is JSON, do not rename it to .bson. That will not convert the format.

Use mongoimport instead:

bash
1mongoimport \
2  --db mydb \
3  --collection users \
4  --file users.json \
5  --jsonArray

Renaming a JSON file to users.bson only changes the filename, not the binary format that mongorestore expects.

Fix 3: Use Archive Mode Properly

If the backup was created as an archive:

bash
mongodump --archive=backup.archive --gzip

Restore it like this:

bash
mongorestore --archive=backup.archive --gzip

Do not point mongorestore at backup.archive as though it were a directory of .bson files. The restore mode has to match the dump mode.

Verify the Backup Type Before Restoring

A good habit is to check how the backup was created before choosing the restore command:

  • 'mongodump output directory means mongorestore <directory>'
  • 'mongodump --archive means mongorestore --archive'
  • 'mongoexport JSON means mongoimport'

Once you keep those pairings straight, this error becomes much easier to avoid.

Common Pitfalls

The most common mistake is assuming any MongoDB backup-like file can be fed to mongorestore. The tool only understands specific dump formats.

Another common issue is leaving unrelated files in the dump directory. A restore tree should be clean, especially when it is copied between systems or extracted from archives.

People also rename files by hand, thinking the extension alone is the format. A .bson filename does not make a JSON file into BSON data.

Finally, archive mode and directory mode are different workflows. If the backup was created with --archive, restore it with --archive.

Summary

  • 'mongorestore expects BSON dump files from mongodump, not arbitrary files.'
  • Extra files in the dump directory can trigger the .bson extension error.
  • JSON exports belong to mongoimport, not mongorestore.
  • Archive dumps must be restored with mongorestore --archive.
  • Check the actual backup format before trying to fix the restore command.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.