Import MongoDB
Overview
MongoDB is a popular NoSQL database that is used in modern web applications for its flexibility and scalability. MongoDB Import tool copies data from a data source like JSON/CSV files into MongoDB collections.
Importing Data in MongoDB
We can import MongoDB data from external sources like JSON, and CSV files using the MongoDB import tool and specifying the location and format of the data. It helps organizations to add new information to an existing collection/database, merge data from various sources, etc.
What is Mongoimport Tool?
MongoDB import is a command-line tool in MongoDB that is used to import data from JSON, CSV, and TSV files into MongoDB collections.
a. Syntax
b. Installation and Compatibility
Installation:
The MongoDB import tool to import MongoDB data can be downloaded as a ZIP archive.
Steps to install Import MongoDB tool:
-
Open https://www.mongodb.com/try/download/database-tools2 and go to MongoDB Command Line Database Tools Download.
Any Version, Platform, and Package can be chosen.
-
Click Download.
-
Unzip the folder.
-
Copy the .exe files from the zipped folder's bin to your computer's file system.
-
To access the MongoDB import tool directly from the command prompt, instead of specifying its full path, we can add the Import MongoDB tool in our system's PATH Environment Variables.
-
Go to System in System and Security of Control Panel.
-
On System Variables section of Environment Variables, we've to click Path and then Edit.
-
Click New and add the filepath to the location from where you copied the MongoDB Import (Database) Tools.
-
Click OK twice to confirm the changes.
Similarly, Import MongoDB can be downloaded using an MSI Installer.
Compatibility:
Officially MongoDB Import tool version 100.7.0 supports several MongoDB Server like MongoDB 6.0, 5.0, 4.4, and 4.2.
c. Required Accesses
To connect to a mongod that enforces authorization, --username and --password options. The user should at least have a readWrite role on the database into which they import MongoDB data.
d. Behavior
Type Fidelity:
During the restoration of data, by default mongoimport uses the JSON format found in the specified target data file.
JSON Format:
mongoimport requires import data to be in either Extended JSON v2.0 (Canonical) or Extended JSON v2.0 (Relaxed) format by default. For import data formatted using Extended JSON v1.0, specify the --legacy option.
Encoding:
Only UTF-8 encoded data files are supported by the MongoDB Import tool. Others will lead to errors.
FIPS:
While connecting to a mongod/mongos which have FIPS mode enabled, the Import MongoDB tool automatically creates a FIPS-compliant connection.
Write Concern:
The --writeConcern value overrides the write concern specified in the URI string if the write concern is specified both in the --writeConcern option and the --uri connection string option.
Batches:
mongoimport supports upto a maximum batch size of 100000 to perform bulk insert/upsert operations.
e. Examples
Simple Import:
In the following example mongoimport imports data from the employees.json file into the employee's collection of the company database:
MongoDB Import Data to a Collection
We can import data into MongoDB collections using the MongoDB import tool.
The following steps are common to import MongoDB data both JSON and CSV files:
-
Open the command prompt(cmd) and type mongod to connect with the MongoDB server and it should be kept open to maintain the connection with the server.
-
Run the Mongo shell using the mongo command in another cmd.
-
Open one more cmd and direct it to the folder which contains the data file which is needed to be uploaded into the collections.
a. Importing JSON File
Syntax:
Steps to import MongoDB data from JSON file data to MongoDB collections using the MongoDB import tool:
-
Here we are going to import a JSON file containing the employee data into the employee's collection in the company database. Using the following command, we can import the JSON file into the collections:
-
We can check the imported data using the Mongo shell. In the Mongo shell, we can find the uploaded documents by first going to the company database by using the use company command and then by using the command db.employees.find().
4 documents have been uploaded to the employee's collection
b. Importing CSV File
MongoDB data from CSV files can be imported using the MongoDB import tool in two ways:
- With header row
- Without header row
i. With header row
Using the --header option, we can import MongoDB data with the header row which is in the first line of the CSV file. Headers are nothing but the name of the fields.
Syntax:
-
Here we are going to import a CSV file containing the employee data into the employee's collection in the company database. Using the following command, we can import the CSV file into the collections:
-
We can check for the uploaded documents following the same steps mentioned we did for the JSON file:
7 documents have been uploaded to the employee's collection
ii. Without header row
Using the MongoDB import tool, we can import MongoDB data without the header row by using the --fields option instead of the --headerline option. After the --fields option, we have to mention
Syntax:
-
Here we are going to import a CSV file containing the employee data into the employee's collection in the company database. Using the following command, we can import the CSV file into the collections:
-
We can check for the uploaded documents following the same steps mentioned we did for the JSON file
7 new documents have been added to the collection.
FAQs
Q: Can data be imported into an existing collection in MongoDB?
A: Yes, data can be imported into an existing collection in MongoDB using the MongoDB import tool.
Q: Can multiple collections be imported simultaneously in MongoDB?
A: Yes, multiple collections can be imported simultaneously using the mongostore command.
Q:. How can we verify if the MongoDB Import tool worked successfully?
A: We can count the number of documents in a collection to verify if the Import MongoDB tool worked successfully. This can be done using the db.collection.count() method in the MongoDB shell.
Conclusion
- We can import MongoDB data using the Import MongoDB tool.
- MongoDB import tool is used to transfer data from external sources like JSON, and CSV files into MongoDB collections.