Using MongoDB Compass
MongoDB Compass is the official graphical user interface (GUI) for MongoDB. It allows you to visually interact with your MongoDB databases and collections without needing to write shell commands.
Installing MongoDB Compass
Compass is usually installed along with MongoDB, but you can also download it separately from the official website:
https://www.mongodb.com/try/download/compass
Step-by-Step: Connecting to a Local MongoDB Server
- Open MongoDB Compass.
- In the connection window, enter the connection string for local MongoDB:
mongodb://localhost:27017
- Click the green Connect button.
Once connected, Compass will display all the available databases in the left sidebar.
Creating a New Database and Collection
- Click on the Create Database button.
- Enter database name:
testdb
- Enter collection name:
users
- Click Create Database
This creates a new MongoDB database called testdb
with a collection called users
.
Inserting Documents with Compass
Now let's insert a document into the users
collection.
- Click on the
testdb
database and open theusers
collection. - Click Insert Document.
- In the editor, paste the following:
{
"name": "Alice",
"age": 28,
"email": "alice@example.com"
}
Click Insert to add the document.
Output:
{ acknowledged: true, insertedId: ObjectId("...") }
Intuition Check
Q: How does Compass know what fields to expect in a document?
A: MongoDB has a flexible schema. It doesn't require predefined fields. Compass simply shows whatever data is stored in each document.
Inserting Multiple Documents
Click Add Data > Insert Document and add the following documents one by one:
{
"name": "Bob",
"age": 35,
"email": "bob@example.com"
}
{
"name": "Charlie",
"location": "New York",
"hobbies": ["reading", "gaming"]
}
Output:
{ acknowledged: true, insertedId: ObjectId("...") }
You now have multiple documents in your collection with different structures — this is totally fine in MongoDB!
Running Queries with Compass
At the top of the collection view, you’ll see a Filter input field. You can enter query conditions here to search your documents.
Example: Find users with age greater than 30
{ "age": { "$gt": 30 } }
Output:
[ { "_id": ObjectId("..."), "name": "Bob", "age": 35, "email": "bob@example.com" } ]
Explanation: This query uses the $gt
(greater than) operator to find documents where the age
is more than 30.
Using Aggregation Pipeline
Compass has a built-in aggregation editor. You can use it to run pipeline queries like grouping and sorting.
Example: Count users by age
[
{ "$group": { "_id": "$age", "count": { "$sum": 1 } } }
]
Output:
[ { "_id": 28, "count": 1 }, { "_id": 35, "count": 1 } ]
Explanation: This groups documents by the age
field and counts how many documents share the same value.
Updating a Document
In the collection view, hover over any document and click the pencil icon to edit.
Change Bob’s age to 36 and save.
Output:
{ acknowledged: true, modifiedCount: 1 }
Deleting a Document
Click the trash icon next to any document to delete it.
Output:
{ acknowledged: true, deletedCount: 1 }
Summary
- MongoDB Compass makes it easy to visually manage databases, collections, and documents.
- You can insert, query, update, and delete documents without writing any shell code.
- Compass is perfect for beginners to explore MongoDB and learn how queries work.
Next Up
Now that you’re comfortable using MongoDB Compass, let’s explore how to perform CRUD operations programmatically using the MongoDB shell.