How to Import .bson File Format on MongoDB?
Last Updated :
04 Jul, 2024
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.
Similar Reads
How to Install MongoDB Bi Connector on MacOS? MongoDB BI connector is a connector for business intelligence that allows you to fabricate different queries using SQL which can be used to envision your data in a more sophisticated way. MongoDB Bi is more advanced than traditional business tools which work only with tabular or flat data. The Mongo
2 min read
Import data to MongoDB MongoDBâs powerful mongoimport tool allows you to easily import data from external files like JSON, CSV, and TSV into your MongoDB collections, making data migration, backup, and integration easier. This utility makes it easy to load data from external sources into MongoDB collections and makes it a
5 min read
How to Install MongoDB on CentOS? MongoDB is a flexible, powerful, and super-fast database management system. Unlike those old-school databases with strict rules, MongoDB lets you store your data in flexible documents, similar to how you organize things in JSON. This means you can easily add new information or change things up witho
4 min read
How to Install MongoDB on Alpine? MongoDB is an open-source NoSQL database. NoSQL databases are quite useful for working with large sets of distributed data. It is a good choice when your data is document-centric and doesn't fit well into the schema of a relational database. It provides full indexing support and replication with ric
2 min read
How to Install MongoDB on Android? MongoDB is a NoSQL document-oriented database used to store data in high volume. Instead of using columns and rows or tables that were used in traditional relational databases like SQL, MongoDB makes use of collections and documents. MongoDB is written in C++ language. MongoDB is developed and manag
9 min read
How to Inserting a Boolean Field in MongoDB In MongoDB, inserting a boolean field (also known as a boolean value) into a document is straightforward and allows you to represent true or false values for specific attributes. This article will explain how to insert boolean fields in MongoDB documents, covering essential concepts and providing be
5 min read