How to Import .bson File Format on MongoDB?
Last Updated :
23 Jul, 2025
BSON (Binary JSON) files play a crucial role in MongoDB for data storage, migration and backup. Understanding how BSON differs from JSON is essential for effectively managing MongoDB databases and importing BSON files.
In this article, we will learn about how to import BSON files into MongoDB using the Mongorestore command-line tool and so on.
Understanding BSON Files
In most cases, BSON files are produced by the Mongodump tool that converts MongoDB data into BSON format. These items correspond to collections within our database and hold the documents in the previous collections. Importing BSON files allows us to:
- Migrate data: Transfer data from one MongoDB instance to another.
- Restore backups: Recover data from a BSON file created through a previous mongodump operation.
- Populate a new database: Add initial data to our development or testing environment.
How is BSON Different from JSON?
| Feature | BSON | JSON |
|---|
| Binary Format | Binary-encoded serialization of JSON-like documents | Text-based format |
| Data Types | Supports additional data types (e.g., dates, binary data) | Basic data types (strings, numbers, arrays, objects) |
| Efficiency | More compact, faster to parse and encode | Less efficient in terms of storage and parsing speed |
| Features | Optimized for MongoDB's storage engine | Simple and widely supported across platforms |
| Usage | Primarily used in MongoDB | Commonly used for data interchange and APIs |
The Mongorestore command-line tool is included with the MongoDB installation and is used to import data from BSON files. Below is the step-by-step tutorial:
Step 1: Open Terminal
Access your command prompt or terminal window.
Step 2: Navigate to the Mongorestore Binary
Use the cd command to navigate to the directory containing the mongorestore executable. This location typically depends on your MongoDB installation method. You can often find it in the bin directory of your MongoDB installation path.
cd /path/to/mongodb/bin
Step 3: Construct the Mongorestore Command
The basic syntax for the mongorestore command is as follows:
mongorestore -d <database_name> <path/to/bson.file>
- Replace
<database_name> with the name of the database you want to import the data into.
- Replace
<path/to/bson.file> with the absolute path to your BSON file.
Step 4: Execute the Mongorestore Command
- Once we have constructed the
mongorestore command, execute it in our terminal. This will initiate the import process and import the BSON file into the specified MongoDB database.
Example: Importing a BSON File
Suppose we have a BSON file named products.bson located in our Downloads directory, and we want to import it into a database named mystore. Here's the command:
mongorestore -d mystore ~/Downloads/products.bson
Explanation: This command will import the data from products.bson into the products collection within the mystore database.
Step 5: Monitor the Import Progress
- During the import process,
mongorestore will display progress updates, including the number of documents imported and any errors encountered.
- Monitor these updates to ensure that the import is proceeding smoothly.
Step 6: Verify the Import
- After the import process is complete, verify that the data has been successfully imported into our MongoDB database.
- We can use the
mongo shell to connect to your database and query the imported data to ensure its integrity.
Advanced Options
mongorestore offers various options for more control over the import process:
- --drop: Drops the existing collection (if any) before importing the data.
- -c <collection_name>: Specifies the specific collection within the BSON file to import. By default, all collections are imported.
- --dryRun: Performs a dry run without actually importing data, just verifying the BSON file's integrity.
Conclusion
Overall,Importing .bson files into MongoDB using the mongorestore command-line utility is a straightforward process, but it requires careful attention to detail. By following the steps outlined above, you can effectively import BSON files into your MongoDB databases and manage your data efficiently.
Explore
MongoDB Tutorial
7 min read
Introduction
Installation
Basics of MongoDB
MongoDB Methods
Comparison Operators
Logical Operators
Arithmetic Operators
Field Update Operators
Array Expression Operators