Menu Close

How to Import a JSON File in MongoDB

How to import a json file in MongoDB

In this tutorial, we will see how to import a JSON file in MongoDB with the help of the MongoDB compass application. To import a JSON file in MongoDB, you must have created the database and collection in MongoDB.
Here collection represents the name of the table where the document will be inserted.

In MongoDB, documents represent the records just like records in SQL Databases like MySQL, PostgreSQL, etc.

In the previous tutorial, we have seen the process of manually inserting a document into the MongoDB collection with the help of the MongoDB compass application and MongoDB command line tool but sometimes you may have a JSON file of the documents and you want to import JSON file in MongoDB collection.

Let’s see the complete process of importing a JSON file into MongoDB with the help of the MongoDB compass application.

What is the MongoDB Compass Application?

MongoDB Compass is a GUI (Graphical User Interface) application that is used to perform almost all database operations, like creating a database, updating the database, deleting a database, creating a collection, deleting a collection and so many operations.

Now, let’s move on to the main topic.

How to Import a JSON File in MongoDB

To import a JSON file in MongoDB using Compass, I have prepared a sample JSON file with some documents as you can see below.

Now, I am about to insert this emp.json file into the employee collection of the ProgrammingFunda database.

[
    {
        "emp_first_name": "Vishvajit",
        "emp_last_name": "Rao",
        "emp_email": "[email protected]",
        "emp_salary": 20000
    },
    {
        "emp_first_name": "Harsh",
        "emp_last_name": "Singh",
        "emp_email": "[email protected]",
        "emp_salary": 24000
    },
    {
        "emp_first_name": "Manok",
        "emp_last_name": "Kumar",
        "emp_email": "[email protected]",
        "emp_salary": 12000
    },
    {
        "emp_first_name": "Vaishali",
        "emp_last_name": "Kumari",
        "emp_email": "[email protected]",
        "emp_salary": 23000
    },
    {
        "emp_first_name": "Nancy",
        "emp_last_name": "Agarwal",
        "emp_email": "[email protected]",
        "emp_salary": 320000
    }
]

To import this JSON, you need to follow some basic steps which are given below.

  • Open MongoDB Compass Application.
  • Click on the Connect button to connect with the server.
How to Import a JSON File in MongoDB
  • Click on the collection name under the Database name, In my case collection name is employee, and the database name is ProgrammingFunda as you can see below.
How to Import a JSON File in MongoDB
  • Now, Click on Import JSON or CSV file under the ADD DATA option.
How to Import a JSON File in MongoDB
  • Choose your JSON file, In my case JSON file name is emp.json and then click on the Import button to import the JSON file.
How to Import a JSON File in MongoDB
  • After clicking on the Import button, your JSON file will be imported into the collection. You can see all your documents in your collection name, as you can see in my case.
How to Import a JSON File in MongoDB

This is how you can import a JSON file into MongoDB using the MongoDB Compass application.

Docs:- Click Here

Conclusion

So, Throughout this article, we have seen all about how to import a JSON file in MongoDB with the help of the MongoDB compass application. MongoDB Compass is a GUI ( Graphical User Interface ) application that allows us to perform almost all database administrative operations.

If you don’t want to use the terminal to import a JSON file in MongoDB then MongoDB will be the best option to import a JSON file.

If you found this article helpful, please share and keep visiting for further MongoDB tutorials.

Thanks for your valuable timeā€¦

How to Insert Documents in MongoDB Collection

Related Posts