MongoDB Compass Practice Questions with Solutions

Introduction

MongoDB Compass is a graphical user interface (GUI) for working with MongoDB databases. It allows you to connect to a MongoDB server, create databases and collections, insert and view documents, filter data, sort results, and inspect database information without writing every command in the terminal. In this chapter, you will practice MongoDB Compass through simple, step-by-step examples designed for beginners. MongoDB Compass Practice Questions with Solutions to help you understand the concepts.

1. Open MongoDB Compass and Connect to a Local MongoDB Server

Problem Statement

You have MongoDB installed on your computer and want to connect MongoDB Compass to your local MongoDB server.

MongoDB Command / Query

In MongoDB Compass, enter the local connection string:

mongodb://127.0.0.1:27017

Then click Connect.

Expected Output

MongoDB Compass opens the database view and displays the databases available on your local MongoDB server.

For example:

Databases

admin
config
local

Explanation

MongoDB Compass provides a graphical way to connect to MongoDB.

The connection string:

mongodb://127.0.0.1:27017

means:

  • mongodb:// → MongoDB connection protocol
  • 127.0.0.1 → your local computer
  • 27017 → default MongoDB server port

Make sure your MongoDB server is running before connecting.


2. View Databases in MongoDB Compass

Problem Statement

After connecting to MongoDB Compass, you want to see the databases available on your MongoDB server.

MongoDB Command / Query

No MongoDB command is required.

In Compass:

  1. Connect to MongoDB.
  2. Look at the Databases section.
  3. You will see the databases available to your user.

Expected Output

You may see databases such as:

admin
config
local

If you have already created other databases, they will also appear.

Explanation

MongoDB Compass displays databases visually.

This is similar to running:

show dbs

in mongosh.

The main advantage of Compass is that you can explore database information without remembering every shell command.


3. Create a New Database and Collection

Problem Statement

Create a database named school and a collection named students using MongoDB Compass.

MongoDB Command / Query

MongoDB Compass does this through its graphical interface.

Steps:

  1. Open MongoDB Compass.
  2. Connect to your MongoDB server.
  3. Click Create Database.
  4. Enter:
Database Name: school
  1. Enter:
Collection Name: students
  1. Click Create Database.

Expected Output

The database appears similar to:

school
 └── students

Explanation

MongoDB stores documents inside collections, and collections belong to databases.

The structure is:

Database
   ↓
Collection
   ↓
Documents

Here:

school
   ↓
students

The students collection can now store student documents.


4. Insert a Document Using MongoDB Compass

Problem Statement

Add a student named Rahul, age 14, studying Python, using MongoDB Compass.

MongoDB Command / Query

In Compass:

  1. Open the school database.
  2. Open the students collection.
  3. Go to the Documents tab.
  4. Click Add Data.
  5. Choose Insert Document.
  6. Enter:
{
  "name": "Rahul",
  "age": 14,
  "course": "Python"
}
  1. Click Insert.

Expected Output

{
  "_id": ObjectId("..."),
  "name": "Rahul",
  "age": 14,
  "course": "Python"
}

Explanation

Compass allows you to insert documents using a graphical editor.

You do not need to run:

db.students.insertOne(...)

manually.

MongoDB automatically adds an _id field if you do not provide one.


5. Insert Multiple Student Documents

Problem Statement

Add three more students to the students collection using MongoDB Compass.

MongoDB Command / Query

Insert these documents through the Compass document editor:

{
  "name": "Priya",
  "age": 15,
  "course": "Java"
}
{
  "name": "Aman",
  "age": 16,
  "course": "JavaScript"
}
{
  "name": "Neha",
  "age": 14,
  "course": "MongoDB"
}

Expected Output

The students collection contains documents similar to:

Rahul   14   Python
Priya   15   Java
Aman    16   JavaScript
Neha    14   MongoDB

Each document also has its own automatically generated _id.

Explanation

MongoDB Compass makes it easy to add documents one at a time.

After inserting the documents, refresh the collection if necessary to see the latest data.


6. Find Students Using a Filter

Problem Statement

You want to display only students whose age is 14.

MongoDB Command / Query

In the Filter field of the Compass Documents tab, enter:

{ "age": 14 }

Then click Find.

Expected Output

{
  "_id": ObjectId("..."),
  "name": "Rahul",
  "age": 14,
  "course": "Python"
}

{
  "_id": ObjectId("..."),
  "name": "Neha",
  "age": 14,
  "course": "MongoDB"
}

Explanation

The filter:

{ "age": 14 }

tells MongoDB Compass to display documents where the age field has the value 14.

This is equivalent to using a query such as:

db.students.find({ age: 14 })

in mongosh.


7. Search for a Student by Name

Problem Statement

Find the student whose name is Aman.

MongoDB Command / Query

Enter the following in the Compass Filter field:

{ "name": "Aman" }

Expected Output

{
  "_id": ObjectId("..."),
  "name": "Aman",
  "age": 16,
  "course": "JavaScript"
}

Explanation

The filter searches for documents where:

name = Aman

The equivalent MongoDB query in mongosh is:

db.students.find({ name: "Aman" })

MongoDB Compass is useful because you can enter the filter directly into the interface and immediately inspect the matching documents.


8. Sort Students by Age

Problem Statement

Display students from the youngest to the oldest using MongoDB Compass.

MongoDB Command / Query

In the Sort field, enter:

{ "age": 1 }

Expected Output

The students are displayed in ascending age order:

Rahul   14
Neha    14
Priya   15
Aman    16

Explanation

MongoDB uses:

1  → Ascending
-1 → Descending

Therefore:

{ "age": 1 }

sorts the students from the lowest age to the highest age.

The equivalent shell query is:

db.students.find().sort({ age: 1 })

9. Display Only Selected Fields

Problem Statement

You want to display only the student’s name and course, without displaying the age field.

MongoDB Command / Query

In the Compass Project field, enter:

{
  "name": 1,
  "course": 1
}

Expected Output

{
  "_id": ObjectId("..."),
  "name": "Rahul",
  "course": "Python"
}

{
  "_id": ObjectId("..."),
  "name": "Priya",
  "course": "Java"
}

Explanation

The Project field in Compass controls which fields should be returned.

Using:

{
  "name": 1,
  "course": 1
}

requests the name and course fields.

The _id field is normally included unless you explicitly exclude it.

For example:

{
  "_id": 0,
  "name": 1,
  "course": 1
}

would exclude _id.


10. Count Documents in a Collection

Problem Statement

You have inserted four students into the students collection. Use MongoDB Compass to check how many documents are present.

MongoDB Command / Query

Open:

school → students → Documents

MongoDB Compass displays the number of matching documents in the collection view.

You can also use the query bar to work with MongoDB commands where supported by your Compass version.

For the equivalent mongosh query:

db.students.countDocuments()

Expected Output

4

Explanation

countDocuments() counts the number of documents that match a query.

When no filter is supplied:

db.students.countDocuments()

counts all documents in the collection.

If you want to count only students aged 14:

db.students.countDocuments({ age: 14 })

the result would be:

2

assuming the sample data from this chapter is used.

Key Takeaways

  • MongoDB Compass is a GUI for MongoDB.
  • Compass lets you work with MongoDB without using the terminal for every operation.
  • You can connect Compass to a local MongoDB server using a connection string.
  • mongodb://127.0.0.1:27017 is a common local connection string.
  • Compass can display databases and collections.
  • You can create databases and collections through the graphical interface.
  • You can insert, view, filter, sort, and project MongoDB documents in Compass.
  • The Filter field is used to find specific documents.
  • The Sort field controls the order of returned documents.
  • The Project field controls which fields are displayed.
  • 1 means ascending sorting and -1 means descending sorting.
  • MongoDB Compass is especially useful for beginners because it provides a visual way to understand databases, collections, and documents.

FAQs

1. What is MongoDB Compass?

MongoDB Compass is a graphical user interface (GUI) that allows you to connect to MongoDB and visually work with databases, collections, documents, queries, indexes, and other database information.

2. Is MongoDB Compass a database?

No. MongoDB Compass is a GUI application for MongoDB. The actual database is managed by the MongoDB server.

3. Do I need MongoDB Compass to learn MongoDB?

No. You can learn MongoDB using mongosh, Compass, or programming languages that connect to MongoDB. Compass is helpful for beginners because it makes database information easier to see and understand.

4. How do I connect MongoDB Compass to a local MongoDB server?

A common local connection string is:

mongodb://127.0.0.1:27017

Enter it into the MongoDB Compass connection screen and click Connect, provided the MongoDB server is running.

5. Can I insert documents using MongoDB Compass?

Yes. Open a collection’s Documents tab, choose Add Data, select Insert Document, enter the document, and insert it.

6. What is the Filter field in MongoDB Compass?

The Filter field is used to specify which documents you want MongoDB to return. For example:

{ "age": 14 }

finds documents where the age field is 14.

7. What is the difference between MongoDB Compass and mongosh?

MongoDB Compass is a graphical interface, while mongosh is a command-line shell. Both can be used to work with MongoDB, but they provide different ways of interacting with the database.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top