Lesson 4 of 50 – MongoDB Compass
8%

MongoDB Compass

MongoDB Compass is a graphical user interface (GUI) for MongoDB. It allows users to connect to databases, explore collections, view documents, create queries, and manage data without typing every command in the MongoDB Shell.

1. What Is MongoDB Compass?

MongoDB Compass is an official graphical tool used to work with MongoDB databases. It is useful for beginners because it presents database information visually.

Using MongoDB Compass, you can:

  • Connect to a MongoDB server.
  • View databases and collections.
  • Insert and edit documents.
  • Search and filter documents.
  • Create and manage indexes.
  • Build aggregation pipelines.
  • View document structures and data types.
Remember: MongoDB Compass is a graphical client. It does not replace the MongoDB database server.

2. Download MongoDB Compass

Follow these steps to download MongoDB Compass:

  1. Open your web browser.
  2. Visit the official MongoDB Compass download page.
  3. Select your operating system.
  4. Choose the appropriate installer or package.
  5. Download and install MongoDB Compass.
  6. Open the application after installation.
Download MongoDB Compass

Use the official website to obtain the latest compatible version for your system.

3. Connect to MongoDB

After opening MongoDB Compass, you can connect to a locally running MongoDB server.

  1. Start your MongoDB server.
  2. Open MongoDB Compass.
  3. Locate the connection string field.
  4. Enter the connection string shown below.
  5. Click Connect.

Local connection string:

mongodb://localhost:27017

The address localhost refers to your own computer, and 27017 is MongoDB's default server port.

If the connection fails, verify that the MongoDB server is installed, running, and listening on the expected port. If authentication is enabled, use the appropriate credentials and connection settings.

4. Understanding the Compass Interface

The main areas of MongoDB Compass include:

1. Connection

Lets you connect to a MongoDB server and manage saved connections.

2. Databases

Displays the databases available to your connection.

3. Collections

Shows collections belonging to a selected database.

4. Documents

Displays documents stored in a collection.

5. Filter

Lets you enter MongoDB query filters to find matching documents.

6. Aggregations

Provides a visual workspace for building aggregation pipelines.

7. Indexes

Displays and helps manage indexes for a collection.

5. Create a Database and Collection

Follow these steps in MongoDB Compass:

  1. Connect to your MongoDB server.
  2. Open the Databases view.
  3. Click Create Database.
  4. Enter school_db as the database name.
  5. Enter students as the collection name.
  6. Confirm by clicking Create Database.

Your database and collection should now be visible in the database list.

Expected structure:

school_db
    |
    |-- students

6. Insert a Document Using Compass

You can add a document to the students collection without using the shell.

  1. Open the school_db database.
  2. Select the students collection.
  3. Open the Documents tab.
  4. Click Add Data and choose Insert Document.
  5. Enter the following document in the JSON editor.
  6. Click Insert.

Example document:

{
    "name": "Rahul",
    "age": 20,
    "course": "ADCA",
    "city": "Aurangabad"
}

Compass may generate an _id field automatically when you insert a document without one.

7. Find Documents Using a Filter

The Filter field lets you search for documents using MongoDB query syntax.

Example 1: Find students from Aurangabad

{
    "city": "Aurangabad"
}

Example 2: Find students aged 20

{
    "age": 20
}

Example 3: Find students aged over 18

{
    "age": {
        "$gt": 18
    }
}

Enter the filter into the collection's Filter field and apply it to display matching documents.

8. Edit and Delete Documents

Edit a document
  1. Open the required collection.
  2. Find the document you want to change.
  3. Use the document's edit control.
  4. Update the required field or value.
  5. Save the change.
Delete a document
  1. Find the document you want to remove.
  2. Use its delete control.
  3. Review the confirmation prompt.
  4. Confirm only if you intend to delete that document.
Warning: Deleting a document removes it from the collection. Always check that you have selected the correct document before confirming.

9. MongoDB Compass vs MongoDB Shell

MongoDB Compass MongoDB Shell
Graphical interface Command-line interface
Uses buttons, tabs, and editors Uses commands and expressions
Useful for visual exploration Useful for scripting and command-based work
Supports visual aggregation building Supports aggregation through commands
Designed for interactive database management Useful for repeatable command workflows

Both tools can connect to the same MongoDB server and operate on the same data.

10. Practice Assignment

  1. Download and install MongoDB Compass.
  2. Connect to your local MongoDB server.
  3. Create a database named practice_db.
  4. Create a collection named employees.
  5. Insert three employee documents.
  6. Find employees whose department is IT.
  7. Edit the city of one employee.
  8. Delete one test document.

🧠 Quick Quiz

Q1. What is MongoDB Compass?




Q2. Which connection string is commonly used for a local MongoDB server?




Q3. Which field is used to filter documents in Compass?