Open In App

How to Import .bson File Format on MongoDB?

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:

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>

Step 4: Execute the Mongorestore Command

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

Step 6: Verify the Import

Advanced Options

mongorestore offers various options for more control over the import process:

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.

Article Tags :