MongoDB stores data in flexible, JSON-like documents. It internally uses BSON (Binary JSON)
MongoDB is an open source NOSQL dataset in C++ -- offers flexible data models, indexing, replication and modern applications for scalable architecture.
Schema-less – Documents can have varying structures. Great for fast development.
Horizontal scaling – Supports sharding (splitting data across servers).
High performance – Optimized for read/write-heavy workloads.
Built-in replication – Supports replica sets for fault tolerance.
Rich query language – Can filter, sort, aggregate, and update nested documents.
Like a “container” for collections.
Example: myAppDB.
Like a table in SQL.
Stores multiple documents.
Example: users, products.
Like a row in SQL but flexible.
Stored in JSON/BSON format.
Can have different fields across documents in the same collection.
Example:
{
"_id": ObjectId("6567e9f1c2f5a3b8e9a0b1c2"),
"name": "Alice",
"age": 28,
"email": "alice@example.com",
"hobbies": ["reading", "cycling"]
}
_id is the unique identifier automatically generated if not provided.
db.users.insertOne({
name: "Alice",
age: 28,
email: "alice@example.com"
})
// Find all users
db.users.find({})
// Find users older than 25
db.users.find({ age: { $gt: 25 } })
db.users.updateOne(
{ name: "Alice" },
{ $set: { age: 29 } }
)
db.users.deleteOne({ name: "Alice" })
Complex queries like SQL GROUP BY.
Example: Count users by age:
db.users.aggregate([
{ $group: { _id: "$age", count: { $sum: 1 } } }
])
Improve query performance.
Example: db.users.createIndex({ email: 1 })