Import JSON and insert JSON into MongoDB

In this MongoDB tutorial, We will learn “How to import JSON into MongoDB and we will also learn how to insert JSON into the MongoDB database. Here you can perform all the operations after inserting into JSON file into the database.

These are the following topics that we are going to cover in this tutorial:

  • How to import JSON into MongoDB and insert into JSON into MongoDB
  • How to import JSON into MongoDB and insert into JSON into MongoDB compass
  • How to import JSON into MongoDB and insert into JSON into MongoDB using python

How to import JSON into MongoDB and insert JSON into MongoDB

What is JSON?

It is an open standard file format. In this, we store data that humans can easily read and transmit data objects consisting of array data types. We use the .json extension to save the file. JSON stands for “JavaScript object notation“.

How to import JSON into MongoDB?

In MongoDB, you can import the JSON file with the help of the mongoimport command.

Syntax:

This Syntax is used to import JSON files into the MongoDB database

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

Here, mongoimport helps you to import JSON files. after that write the database name, collection name, and file location(path) that you want to import into your database.

Follow these steps to import JSON file into MongoDB database:

  • Open command command prompt, start the MongoDB server
how we can start mongodb server in windows
start MongoDB server
  • This is the JSON(student.json) file that we are going to import into database
import json in MongoDB
import student.json in MongoDB
  • Again open command prompt till bin folder and write the following command and execute it.
C:\Program Files\MongoDB\Server\5.0\bin>mongoimport --jsonArray --db mydata --collection student --file F:\students.json
Import JSON file into MongoDB
Import JSON file into MongoDB
  • Now the student.json file is successfully imported in MongoDB database.
  • You can check the database in MongoDB with show dbs command this is show you all the databases that you have created into MongoDB database.
> show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mydata      0.000GB

Here, you can see the mydata database is successfully created in the MongoDB database.

How to check inserted JSON in MongoDB?

For checking the inserted JSON, All the data goes into collection. For checking the data inserted into JSON file follow these steps:

  • The show collections is use to check the collection that how many collection in your database.
  • Here only collection in the database, the student is the collecion name that we created earlier.
> use mydata
switched to db mydata
> show collections
student
  • For checking the data inside the collection check by using this command
> db.student.find().pretty()
{
        "_id" : ObjectId("611f662e3a6c1c3ffb3edc65"),
        "name" : "John",
        "age" : 21
}
{
        "_id" : ObjectId("611f662e3a6c1c3ffb3edc66"),
        "name" : "Harry",
        "age" : 21
}
{
        "_id" : ObjectId("611f662e3a6c1c3ffb3edc67"),
        "name" : "Emma",
        "age" : 21
}
{
        "_id" : ObjectId("611f662e3a6c1c3ffb3edc68"),
        "name" : "Peter",
        "age" : 21
}

Here, you can see data(JSON file) inserted successfully into the student collection.

Read: How to check if MongoDB is installed + MongoDB Version

How to import JSON into MongoDB and insert JSON into MongoDB compass

In MongoDB compass, for importing the JSON file and to see what you have inserted into the database using JSON you need to follow the following steps:

  • Open MongoDB compass
  • First of all you need to create a new database manully so click on create database button.
create a database in MongoDB compass
create a database in MongoDB compass
  • After clicking on the button, this will open a new tab.
  • Here you have to write database and collection name and simply click on Create Database button.
create a  new database in MongoDB compass
create a new database in MongoDB compass

Here, we gave database and collection names as database and student respectively.

  • you can see, database and collection are created succesfully.
create database in MongoDB compass
create a database in MongoDB compass
  • Now you have to insert the JSON file into MongoDB compass so click on collection(student) that you have created.
  • Click on import data button and select file that you want to import(JSON file) also select the input file type and click on import button.
Import JSON file into MongoDB compass
Import JSON file into MongoDB compass
  • This is the simplest way of importing the JSON file, After importing the data, you can easily access the data.
Import JSON file into MongoDB compass
datasets after insert JSON file

Here, we successfully inserted JSON files into the MongoDB compass.

Read: MongoDB sort by date

How to import JSON into MongoDB and insert JSON into MongoDB using python

For import JSON in MongoDB using python, you need to first import the JSON and pymongo library. With the help of this library, you can easily open the JSON file and make the connection between Python and MongoDB respectively.

After that, the class MongoClient enables you to make the connection with the MongoDB server. By default, MongoDB is run on 27017 port.

Here mydatabase and data are database and collection names, we store it into variables db and Collection respectively. But, you can use any name for the variables. Now to open the JSON file define the location of the JSON file and here the load() function will open the JSON file.

Example:

In this example, you will learn how to import JSON files and insert JSON file into MongoDB

import json
import pymongo 
  
  
# Making Connection
myclient = pymongo.MongoClient("mongodb://127.0.0.1:27017/") 
   
# database 
db = myclient["mydatabase"]
   
# Created or Switched to collection 

Collection = db["data"]
  
# Loading or Opening the json file
with open('E:/data.json') as file:
    file_data = json.load(file)
      
# Inserting the loaded data in the Collection
# if JSON contains data more than one entry
# insert_many is used else inser_one is used
if isinstance(file_data, list):
    Collection.insert_many(file_data)  
else:
    Collection.insert_one(file_data)
Import JSON file into MongoDB using python
Import JSON file into MongoDB using python

After successfully executing the above code, you can easily access the data from the MongoDB database.

>show dbs
admin       0.000GB
config      0.000GB
database    0.000GB
mydatabase  0.000GB

The “show dbs” command will show you all the databases that you have created into your MongoDB database. Here, you can also see mydatabase is successfully created.

> use mydatabase
switched to db mydatabase
> show collections
data

The “show collections” command will show you all the collections that you have created into your database. Here, data is the collection that we created earlier.

> db.data.find().pretty()
{"_id": 1, "name" : "Smith", "class" : 11, "subject" : "Science/Maths"}
{"_id": 2, "name" : "Tom", "class" : 12, "subject" : "Arts"}
{"_id": 3, "name" : "Adam", "class" : 12, "Branch" : "Commerce/Maths"}

Now, you can see the data that was in the data.json file. That is the easiest way to stored in the collection using python code.

You may also like reading the following articles.

In this tutorial, you have learned How to import JSON into MongoDB and you have also learned how to insert JSON into the MongoDB database.

These are some of the topics that we covered in this tutorial:

  • How to import JSON into MongoDB and insert into JSON into MongoDB
  • How to import JSON into MongoDB and insert into JSON into MongoDB compass
  • How to import JSON into MongoDB and insert into JSON into MongoDB using python

Leave a Comment