MongoDB is one of the most popular NoSQL databases today, known for its speed, flexibility, and developer-friendly syntax. Whether you're a backend developer or just exploring NoSQL databases, this guide will walk you through MongoDB fundamentals with hands-on shell commands.
What is MongoDB?
MongoDB is a document-oriented, NoSQL database built for speed, scalability, and developer productivity. Here are its key characteristics:
- Document-Oriented: Stores data in JSON-like documents (BSON format)
- Object-Oriented: Works well with object-based applications; no ORM needed
- Non-Relational: No tables or rows — collections and documents instead
- Flexible Schema: Dynamic schema — perfect for unstructured or evolving data
- Highly Scalable: Built for horizontal scaling across servers
- JS-Based Queries: Use familiar JavaScript syntax for querying
MongoDB listens on port 27017 by default.
Installing MongoDB (Windows)
You can run MongoDB locally or inside Docker. For a local Windows setup:
- Download the MongoDB installer (
.msi), Zip Package, or Mongo Shell (mongosh) from the official site - Create the required directories:
C:\data\db C:\data\log - Add the MongoDB
binfolder to your systemPATHenvironment variable
Running MongoDB
Step 1 — Start the server (keep this terminal open):
mongod
Step 2 — Open the shell in a new terminal:
mongosh
You're now connected and ready to interact with your data.
Collections and Documents
MongoDB uses different terminology compared to relational databases:
| MongoDB | Relational DB |
|---------------|---------------|
| Collection | Table |
| Document | Row |
| _id field | Primary Key |
Basic Commands
// Show all non-empty databases
show dbs
// Create or switch to a database
use book_db
// Create a collection
db.createCollection("book")
// View all collections
db.getCollectionNames()
Inserting Documents
// Insert with auto-generated _id
db.book.insert({
title: "Angular Programming",
author: "Harsh Kumar",
price: 545.0
})
// Insert with a manual _id
db.book.insert({
_id: 2,
title: "Java Programming",
author: "Gaurav Sharma",
price: 745
})
// Insert with extra fields
db.book.insert({
_id: 3,
title: "MongoDB",
price: 745,
publishedYear: 2020
})
Querying Documents
// View all documents
db.book.find()
// Pretty-print results
db.book.find().pretty()
// Filter by a field value
db.book.find({ price: 750 })
// OR condition
db.book.find({ $or: [{ _id: 2 }, { title: "Java Programming" }] })
// Greater than ($gt)
db.book.find({ price: { $gt: 800 } }).pretty()
Updating Documents
// Update using $set
db.book.update(
{ title: "Python for Beginners" },
{ $set: { title: "Angular Programming", price: 650 } }
)
// Update a single field by _id
db.book.update({ _id: 3 }, { $set: { price: 650 } })
// Save — inserts if _id doesn't exist, updates if it does
db.book.save({
_id: 100101,
title: "Java Programming",
price: 890
})
Deleting Documents
// Delete by _id
db.book.remove({ _id: 4 })
// Delete by field value
db.book.remove({ title: "React JS" })
Admin Commands
// Count documents in a collection
db.book.find().count()
// Drop a collection
db.book.drop()
// Drop the current database
db.dropDatabase()
Creating Users
use order_db
db.createUser({
user: "ramanuj",
pwd: "password",
roles: [
{ role: "readWrite", db: "order_db" }
]
})
This grants ramanuj read/write access to order_db.
Summary
By the end of this guide you've covered:
- What MongoDB is and how it differs from traditional RDBMS
- How to install and run MongoDB locally
- Core concepts: databases, collections, documents, and
_id - Full CRUD operations using the MongoDB shell
- How to create users and manage database access
This is a solid starting point for developers moving from relational databases or exploring NoSQL for the first time.