MongoDB

Working with MongoDB Compass

In this article on Working with MongoDB Compass, I will discuss MongoDB Compass which is a GUI client for managing the MongoDB database.

MongoDB Compass

Basically, the Compass is a simple and easy-to-use GUI that you can use to visualize and explore the MongoDB database. MongoDB Compass is used for database administration and querying. Furthermore, you can perform queries as well as insert, update, and delete documents using its interface.

When you install MongoDB, the installation wizard shows the option to install MongoDB Compass. So, you need to check that option during MongoDB installation. After the installation is finished, you can launch the Compass. The following interface opens. Next, you can click on the Connect button.

Working with MongoDB Compass - MongoDB Compass Interface
Working with MongoDB Compass – MongoDB Compass Interface

When the Compass is connected with the MongoDB server, the left navigation panel shows the list of databases as shown below.

Databases Shown in MongoDB Compass
Databases Shown in MongoDB Compass

In order to create a database, click on the Create Database button.

Creating a MongoDB Database
Creating a MongoDB Database

In the dialog box that appears next, you can specify the database name. Also, you need to specify the collection name as well.

Create Database and Collection Using MongoDB Compass
Create Database and Collection Using MongoDB Compass

When you click on the Create Database button, both the database and the collection appear on the left navigation panel.

Database and the Collection in MongoDB
Database and the Collection in MongoDB

After that, click on the collection name. It displays the interface to add documents to the collection. The following figure shows the interface to add data to the collection.

Adding Data in the Collection
Adding Data in the Collection

From the Add Data drop-down list you can choose the option to import the file containing data.

It displays the option to import a JSON file or a CSV file.

Importing a JSON or CSV file in a MongoDB Collection
Importing a JSON or CSV file in a MongoDB Collection

Now let us create a JSON file containing some data. The file is shown below.

data.json

[ {"emp_id": 111, "emp_name": "Jim"}, {"emp_id": 112, "emp_name": "Mark"}, {"emp_id": 113, "emp_name": "Jill"}]

Click on the JSON option and then click on Select a File… option.

Select a JSON File
Select a JSON File

When you click on the Import button, the file is imported and the collection shows the documents.

Documents in a Collection in a MongoDB Database
Documents in a Collection in a MongoDB Database

Further Reading

Getting Started With MongoDB

How to create a MongoDB database using PHP Script?

Create a collection in a MongoDB database in PHP

Insert Documents in a Collection

Show All Documents in a Collection

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *