Basic MongoDB Commands: mongo, mongosh, and GUI Tools
A Super Simple, Fun, and Powerful Guide for Everyone
Imagine MongoDB as a magic notebook where you can write anything—stories, drawings, lists, or even secret codes. You don’t need to follow strict rules like in a school register. This MongoDB commands tutorial teaches you how to talk to MongoDB using simple commands and cool tools.Whether you're just starting or a pro developer, this guide is made for you!
We’ll cover:
- mongo (old shell still used sometimes)
- mongosh (new, modern shell, the future!)
- GUI Tools (like clicking buttons instead of typing!)
Let’s begin!
Part 1: What Are These Tools?
| Tool | What It Is | Best For |
|---|---|---|
| mongo | Old command-line shell | Legacy systems, quick checks |
| mongosh | New and improved shell (2021+) | Everyone! Modern, colorful, smart |
| GUI Tools | Click-and-play apps (like Compass) | Beginners, visual learners, fast testing |
Fun Fact: mongosh = MongoDB Shell. It’s like upgrading from a bicycle to a rocket bike!
Part 2: Starting with mongosh (The Best Way!)
Step 1: Open mongosh
After installing MongoDB (see our previous tutorial), open your terminal (Linux/macOS) or Command Prompt/PowerShell (Windows).
Type:
mongosh
You’ll see something like this:
Current Mongosh Log ID: 671a1f2c...
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true
Using MongoDB: 7.0.5
Using Mongosh: 2.2.12
test>
Beginner Tip: The > is your cursor. Type commands here and press Enter!
Basic Commands You Must Know
Let’s learn with a school example! We’ll store student info.
1. Switch to a Database
use school
Creates or switches to school database. Like opening a new notebook labeled "School".
Pro Tip: Even if it doesn’t exist, MongoDB creates it when you save data!
2. Insert a Student (Add Data)
db.students.insertOne({
name: "Rohan",
age: 13,
class: "8th",
hobbies: ["cricket", "drawing"]
})
Output:
{
acknowledged: true,
insertedId: ObjectId("671a2f1d...")
}
Beginner Example: You just added a new student card to your magic notebook!
Expert Insight: insertOne() is atomic. Safe for one record. Use insertMany() for bulk.
3. Find Students (Search)
db.students.find()
Output:
{
"_id": ObjectId("671a2f1d..."),
"name": "Rohan",
"age": 13,
"class": "8th",
"hobbies": ["cricket", "drawing"]
}
Pretty Print (Easier to Read):
db.students.find().pretty()
4. Find Specific Students
db.students.find({ age: 13 })
db.students.find({ "hobbies": "cricket" })
Beginner Example: Like asking, “Show me all kids who play cricket.”
Expert Tip: Use operators:
$gt → greater than
$in → in a list
$ne → not equal
db.students.find({ age: { $gt: 12 } })
5. Update a Student
db.students.updateOne(
{ name: "Rohan" },
{ $set: { class: "9th", age: 14 } }
)
Beginner Example: Rohan got promoted! We updated his class.
Expert: Use $inc to increase numbers:
db.students.updateOne(
{ name: "Rohan" },
{ $inc: { age: 1 } }
)
6. Delete a Student
db.students.deleteOne({ name: "Rohan" })
Careful! This removes the student forever.
Safer Way (for testing):
db.students.deleteMany({ class: "temp" })
7. List All Databases
show dbs
8. List Collections (Tables)
show collections
9. Drop (Delete) a Collection
db.students.drop()
10. Drop Entire Database
db.dropDatabase()
Warning: Only do this if you’re sure!
Part 3: mongo vs mongosh: What’s the Difference?
| Feature | mongo (Old) | mongosh (New) |
|---|---|---|
| Release Year | 2011 | 2021 |
| Syntax Highlighting | No | Yes (colorful!) |
| Auto-complete | Basic | Smart (Tab = magic!) |
| JavaScript Support | Limited | Full ES6+ |
| Multi-line Editing | Hard | Easy (like code editor) |
| Built-in Help | help | ? or .help |
| Future-Proof | Deprecated | Officially Supported |
Bottom Line: Always use mongosh!
mongo is like an old phone. It works, but why not use a smartphone?
Part 4: GUI Tools: Click Instead of Type!
1. MongoDB Compass (Free & Official)
Download: mongodb.com/compass
How to Use (Step-by-Step)
- Open Compass
- Connect to Localhost
- Connection String:
mongodb://localhost:27017 - Click Connect
See Your Data Visually!
Click on school → students
You’ll see Rohan’s data in a table!
Click to Edit, Delete, or Add
No typing needed!
Beginner Win: Perfect for learning without fear of typos.
Expert Win: Export/import JSON, analyze indexes, build aggregation pipelines visually.
2. VS Code + MongoDB Extension
For Coders Who Love VS Code
- Install MongoDB for VS Code extension
- Connect to
mongodb://localhost:27017 - Write queries in
.mongodbfiles with syntax highlighting
// students.mongodb
use school
db.students.find({ age: { $gte: 13 } })
Run with: Ctrl + Alt + Enter
Pro Tip: Save queries, share with team, version control with Git!
3. MongoDB Atlas (Cloud) + Web Shell
No installation needed!
Go to cloud.mongodb.com
Create free cluster
Click "Open MongoSH" → Web-based mongosh
Beginner: Try MongoDB in your browser!
Expert: Deploy, scale, monitor from one dashboard.
Part 5: Mini Project: Build a "Class Diary"
Let’s practice everything!
Step 1: Create Diary
use classDiary
Step 2: Add Entries
db.entries.insertMany([
{
date: "2025-10-24",
title: "Science Fair",
mood: "excited",
highlights: ["Won 1st prize!", "Robot worked!"]
},
{
date: "2025-10-23",
title: "Rainy Day",
mood: "calm",
highlights: ["Read Harry Potter", "Ate Maggi"]
}
])
Step 3: Query Fun
// Find happy days
db.entries.find({ mood: "excited" })
// Find entries with "prize"
db.entries.find({ highlights: "Won 1st prize!" })
// Update mood
db.entries.updateOne(
{ title: "Rainy Day" },
{ $set: { mood: "cozy" } }
)
Try in Compass too! See the data as a table.
Part 6: Cheat Sheet (Print & Stick!)
| Command | What It Does |
|---|---|
| mongosh | Open modern shell |
| use dbname | Switch/create database |
| db.collection.insertOne({}) | Add one record |
| db.collection.find() | Show all data |
| db.collection.find({key: value}) | Search |
| db.collection.updateOne(filter, update) | Change data |
| db.collection.deleteOne({}) | Remove one record |
| show dbs | List databases |
| show collections | List tables |
| db.dropDatabase() | Delete current DB |
Part 7: Tips for All Levels
For Beginners
- Start with Compass – no typing!
- Use
pretty()to read output easily - Make a "Game Scores" or "Pet Diary" project
For Medium Learners
Practice aggregation:
db.students.aggregate([
{ $match: { age: { $gte: 13 } } },
{ $group: { _id: "$class", count: { $sum: 1 } } }
])
Learn indexing:
db.students.createIndex({ name: 1 })
For Experts
Use transactions (MongoDB 4.0+):
const session = db.getMongo().startSession()
session.startTransaction()
// ... operations
session.commitTransaction()
Monitor with mongostat, mongotop
Connect via drivers (Python, Node.js)
Final Words
You now know how to talk to MongoDB like a pro!
Use mongosh for power and fun
Use Compass for visual magic
Build projects to remember forever
Your Next Step:
use myWorld
db.me.insertOne({ name: "You", skill: "MongoDB Hero", level: "Beginner → Expert" })
You did it!
Resources:
Keep exploring! The world of data is waiting for you.
💡 Have questions or want me to cover MongoDB queries or Atlas setup next? Drop a comment below!
No comments:
Post a Comment