Quick Guide to Import Data in MongoDB

Import Data In MongoDB Featured Image

In this tutorial, I am going to discuss how to import data in MongoDB using a tool named mongoimport offered by Mongo itself.

Importing is a facility that most databases and language frameworks provide. This is done to make the data exported from other applications, databases, or languages, usable by other programs, applications, or languages in various forms.

The common file types that are imported and exported for use in a database are TSV, CSV, BSON, and JSON.

MongoDB allows developers to achieve this with the help of the mongoimport tool. The mongoimport tool must be executed outside the mongo shell as it cannot work there. This is a system command-line tool.

Normally, this tool resides in the bin folder of MongoDB. However, if you do not find it there, you can simply follow the below steps mentioned in the mongoimport installation section to get you going.

This tutorial will throw light on how we can import data in MongoDB from CSV (Comma Separated Value), TSV, and Extended JSON file types using the mongoimport CLI tool. Let us first learn in brief about the mongoimport tool.

What is the mongoimport CLI Tool in MongoDB?

The mongoimport tool helps in importing data from MongoDB’s mongoexport CLI tool or any other third-party export tool. It allows developers to import data of a JSON, TSV, or CSV file type. Since the MongoDB 4.4 launch, mongoimport tool is being offered separately from the MongoDB Server. It hence uses its own versioning.

The mongoimport version that we will use today, is 100.5.0. The first released version of the mongoimport tool used to import data in MongoDB, was 100.0.0. It was then released together with the MongoDB Server and adopted similar versioning.

The mongoimport CLI tool version 100.5.0 supports the below versions of MongoDB:

  • MongoDB 5.0
  • MongoDB 4.4
  • MongoDB 4.2
  • MongoDB 4.0

Let us now proceed with installing the mongoexport tool. The mongoimport tool is offered with the MongoDB Database Tools package.

Installing mongoimport CLI Tool

Follow the steps below to install mongoimport from the official MongoDB website to help import data in MongoDB.

  • Visit the official downloads page of MongoDB.
  • Under the headline “Choose which type of deployment is best for you”, click Tools.
  • Scroll down to find the MongoDB Database Tools accordion and pull it open.
  • Under the Available Downloads form, select the operating system of your machine and the type of installation you prefer.
  • If you have chosen to download the installer, customize your installation and follow the installation process. After this, you can proceed to use the mongoimport tool.
  • If you choose to download the mongodb-database-tools-windows-x86_64-100.5.1.zip file, copy all the files of the folder.
  • Next, jump to the bin folder of MongoDB in your system and paste all the copied files into this folder.
  • You will now be able to use the mongoimport CLI tool to import data in MongoDB.

Importing JSON Data into MongoDB

The JSON data type is used quite often by developers. To import data in MongoDB of a JSON type, let us take a quick look at the syntax:

mongoimport –jsonArray –db database_name –collection collection_name –file file_location

Let us get started with the steps to import JSON data into a MongoDB database.

  • Start up your MongoDB server
  • Now, open the Mongo shell, so we can use the mongoimport CLI tool here
  • Next, open up your system’s terminal and provide the absolute path of the bin folder of MongoDB. It might look something like this. However, it might depend where you have installed MongoDB on your machine:
C:\mongodb\bin
  • Next, let us suppose we have a JSON file that we want to import into a Mongo database:
[
    {
        "city": "Delhi",
        "latitude": 28.6600,
        "longitude": 77.2300,
        "country": "India",
        "iso2": "IN",
        "state": "Delhi",
        "capital": "admin",
        "population": "29617000",
        "population_proper": "16753235"
    },
    {
        "city": "Mumbai",
        "latitude": 18.9667,
        "longitude": 72.8333,
        "country": "India",
        "iso2": "IN",
        "state": "Maharashtra",
        "capital": "admin",
        "population": "23355000",
        "population_proper": "12478447"
    },
    {
        "city": "Kolkata",
        "latitude": 22.5411,
        "longitude": 88.3378,
        "country": "India",
        "iso2": "IN",
        "state": "West Bengal",
        "capital": "admin",
        "population": "17560000",
        "population_proper": "4496694"
    },
    {
        "city": "Bangalore",
        "latitude": 12.9699,
        "longitude": 77.5980,
        "country": "India",
        "iso2": "IN",
        "state": "Karnataka",
        "capital": "admin",
        "population": "13707000",
        "population_proper": "8443675"
    },
    {
        "city": "Chennai",
        "latitude": 13.0825,
        "longitude": 80.2750,
        "country": "India",
        "iso2": "IN",
        "state": "Tamil Nadu",
        "capital": "admin",
        "population": "11324000",
        "population_proper": "6727000"
    }
]
  • We will now import data in MongoDB using the mongoimport tool. To do so, simply pass the below command as per your requirements:
mongoimport --jsonArray --db heathersTerrarium --collection locations --file C:\locations.json
  • Next, to check if the JSON data was imported, find all documents in the collection in your database. First, move into the database you want to use:
show dbs
use heathersTerrarium
  • Now, print all the documents in the mongo shell:
db.locations.find({}).pretty()
{
        "_id" : ObjectId("6054f5397c7f6b1274b80765"),
        "city": "Delhi",
        "latitude": 28.6600,
        "longitude": 77.2300,
        "country": "India",
        "iso2": "IN",
        "state": "Delhi",
        "capital": "admin",
        "population": "29617000",
        "population_proper": "16753235"
    },
    {
        "_id" : ObjectId("6054f5397c7f6b1274b874839"),
        "city": "Mumbai",
        "latitude": 18.9667,
        "longitude": 72.8333,
        "country": "India",
        "iso2": "IN",
        "state": "Maharashtra",
        "capital": "admin",
        "population": "23355000",
        "population_proper": "12478447"
    },
    {
        "_id" : ObjectId("6054f5397c7f6b1274b85352e9"),
        "city": "Kolkata",
        "latitude": 22.5411,
        "longitude": 88.3378,
        "country": "India",
        "iso2": "IN",
        "state": "West Bengal",
        "capital": "admin",
        "population": "17560000",
        "population_proper": "4496694"
    },
    {
        "_id" : ObjectId("6054f5397c7f6b1274b86732b45"),
        "city": "Bangalore",
        "latitude": 12.9699,
        "longitude": 77.5980,
        "country": "India",
        "iso2": "IN",
        "state": "Karnataka",
        "capital": "admin",
        "population": "13707000",
        "population_proper": "8443675"
    },
    {
        "_id" : ObjectId("6054f5397c7f6b1274b8625d63"),
        "city": "Chennai",
        "latitude": 13.0825,
        "longitude": 80.2750,
        "country": "India",
        "iso2": "IN",
        "state": "Tamil Nadu",
        "capital": "admin",
        "population": "11324000",
        "population_proper": "6727000"
    }

Perfect! We have successfully used the mongoimport CLI tool to import data in MongoDB for a JSON data.

Importing CSV Data into MongoDB

The steps to import CSV data is quite similar. Here’s the syntax:

mongoimport –db database_name –collection collection_name –type csv –fields field_names –file file_location

Simply follow the steps below to import data in MongoDB using mongoimport for CSV data type.

This is how our locations.csv data looks like:

city,country
Mumbai,India
Phuket,Thailand
Moscow,Russia
Dubai,UAE
NYC,USA
  • Importing the above CSV data to our Mongo database using mongoimport:
mongoimport –db heathersTerrarium –collection locations –type csv –fields city,country –file C:\locations.csv
  • Now, you can simply print out the documents in the Mongo shell like before to check if the CSV data is imported.

Read more: Quick Guide to Export All Collections in MongoDB [2021]

Conclusion

In this article, we learned how to import data in MongoDB using the mongoimport CLI tool by MongoDB.

Noteworthy References

https://docs.mongodb.com/database-tools/mongoimport/