Open In App

How to Import .bson File Format on MongoDB?

Last Updated : 10 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

BSON (Binary JSON) files play a crucial role in MongoDB as they are used to store data within collections. These files are particularly useful for tasks such as migrating data between MongoDB instances restoring backups, and populating new databases.

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 to Import .bson File Format on MongoDB?

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads