• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:8 mins read
You are currently viewing Import JSON file in MongoDB

A mongoimport command is a tool provided by MongoDB that allows users to import data from various file formats, including CSV, TSV, and JSON. The mongoimport command has a wide range of options that allow users to configure how the data is imported.

Advertisements

In this article, we’ll go over the mongoimport command’s ability to import JSON files with the help of illustrative examples.

1. Syntax of mongoimport command

Before diving into the examples, let’s first look at the basic syntax of the mongoimport command.


# Syntax
mongoimport --db dbname --collection collectionname --file filepath

Here,

  • --db flag takes the database name
  • --collection is used to set the collection name where the data will be imported, and the
  • --file represents the filepath which is the path to the JSON file that contains the data.

2. Import JSON into MongoDB Collection using mongoimport Command

You can use the mongoimport command to import the JSON data into a particular collection in MongoDB. Consider the following data in a student.json file which I will use to demonstrate the import.


# Consider JSON data in a student.json file
{
  "_id": 1,
  "name": "Elena Gilbert",
  "email": "[email protected]"
}
{
  "_id": 2,
  "name": "Alaric Steven",
  "email": "[email protected]"
}

Now, we have a mongoimport command where the --db option specifies the name of the database MyDatabase to import the data. The —collection option specifies the name of the collection student to import the data. Then, the —file option represents the JSON file student.json name to import along with the path where it is stored.  


# Importing JSON data into collection
mongoimport --db MyDatabase --collection student --file C:\Users\Hp\Desktop\student.json

When this mongoimport command is executed in the prompt, it displays the documents that are imported into the specific collection like below. 

Import JSON file mongoimport command

3. Importing JSON data using upserts

As mongoimport command has many other options that help import the JSON data into the MongoDB collection. One of them is the upserts option. To demonstrate this let’s use the JSON file enrolls.json with the following data.


# Create JSON file
{
  "_id": 1,
  "course": "Python",
}
{
  "_id": 2,
  "course": "Java",
}

Now, we let’s import the above JSON file into the MongoDB database by using the following mongoimport command. Hereafter specifying the database and the collection refers to enrollments.

Then, we use the –upserts flag which represents that any available records will be updated and new records will be added. The --upsertFields option is also set which represents the _id field as the unique identifier for upserts.  


# Importing JSON data using upserts
mongoimport --db MyDatabase --collection enrollments --file C:\Users\Hp\Desktop\enroll.json --upsert --upsertFields _id

The output of the above command verifies that JSON format documents are imported into the MongoDB database.

mongoimport command in MongoDB

4. Importing JSON data with a unique batch size

Sometimes, we have a large data in the JSON file that needs to be imported into the MongoDB collection in batches using Mongoimport. The batchSize option determines the quantity of documents imported in each batch, thereby enhancing performance and lowering memory consumption.

Suppose that we have a JSON file course.json that contains 1000 documents. Now, in the following mongoimport command, we have imported 500 documents at a time by specifying the 500 value to the batchSize option. By default, mongoimport uses a batch size of 1000 documents.


# Importing JSON data with a unique batch size
mongoimport --db MyDatabase --collection course --file C:\Users\Hp\Desktop\course.json --batchSize 500

The output represents that 500 documents of JSON files are imported within the MongoDB collection.

mongoimport with batchsize MongoDB

5. Importing Array of JSON data in MongoDB

When a file has multiple JSON documents, we can use the --jsonArray option to represent that the file contains a JSON array. Also, the mongoimport should treat each element in the array as a separate document to be imported. Suppose that we have the following JSON file which consists of the array document.


# Array of JSON data
[
  {
    "name": "John",
    "age": "25"
  },
  {
    "name": "Jane",
    "age": "30"
  }
]

Now, we are using the mongoimport command below to import the JSON file blogs.json into the collection studentblogs. We have used the option --jsonArray to specify that a particular file has the array. Then, we have set the –parseGrace option with the value skip to skip any documents that have errors and continue with the import process.


# Importing JSON data contains an array
mongoimport --db MyDatabase --collection studentBlogs --file C:\Users\Hp\Desktop\blogs.json --jsonArray --parseGrace "skip"

Here, the output approved that JSON array formatted documents are imported into the collection of MongoDB.

Import json MongoDB

6. Conclusion

In conclusion, the mongoimport command is effective to import JSON data into MongoDB collection quickly and easily. By using the various options available in mongoimport, we can customize the import process to accommodate the needs and ensure that the data is imported correctly.

More details about the mongoimport command can be found here.