⬅ Previous Topic
Creating a Database and Collection in MongoDBNext Topic ⮕
Using MongoDB Shell⬅ Previous Topic
Creating a Database and Collection in MongoDBNext Topic ⮕
Using MongoDB ShellMongoDB 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.
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
mongodb://localhost:27017
Once connected, Compass will display all the available databases in the left sidebar.
testdb
users
This creates a new MongoDB database called testdb
with a collection called users
.
Now let's insert a document into the users
collection.
testdb
database and open the users
collection.
{
"name": "Alice",
"age": 28,
"email": "alice@example.com"
}
Click Insert to add the document.
Output:
{ acknowledged: true, insertedId: ObjectId("...") }
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.
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!
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.
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.
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 }
Click the trash icon next to any document to delete it.
Output:
{ acknowledged: true, deletedCount: 1 }
Now that you’re comfortable using MongoDB Compass, let’s explore how to perform CRUD operations programmatically using the MongoDB shell.
⬅ Previous Topic
Creating a Database and Collection in MongoDBNext Topic ⮕
Using MongoDB ShellYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.