MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. Itโs schema-less, meaning the structure of the data can evolve over time.
Letโs see how RDBMS and MongoDB differ:
- ๐ Document-oriented (stores data in JSON-like documents).
- ๐ High scalability and performance.
- ๐๏ธ Schema-less, meaning flexible data structures.
- ๐ High availability through replication.
- ๐ ๏ธ Supports complex queries with the Aggregation Framework.
- Visit the MongoDB Download Center.
- Select your operating system (Windows, macOS, or Linux) and download the appropriate version.
- For Windows:
- Run the
.msi
file and complete the setup. - Add the MongoDB
bin
folder to your system PATH for easy access.
- Run the
- For macOS (Homebrew):
brew tap mongodb/brew brew install mongodb-community
- For Linux:
Use your package manager, e.g.,
apt
for Ubuntu:sudo apt update sudo apt install -y mongodb
-
Start the MongoDB server:
mongod --dbpath <path_to_data_directory>
Replace
<path_to_data_directory>
with the path where you want to store data. -
Connect to the MongoDB shell:
mongo
CRUD stands for Create, Read, Update, and Delete. Letโs use a collection named students
.
Use the insertOne()
or insertMany()
methods to add data.
// Insert one document
use school;
db.students.insertOne({ name: "Rajesh Kumar", age: 20, city: "Delhi" });
// Insert multiple documents
db.students.insertMany([
{ name: "Priya Sharma", age: 19, city: "Mumbai" },
{ name: "Amit Singh", age: 22, city: "Bangalore" }
]);
Query data using the find()
or findOne()
methods.
// Find all documents
db.students.find();
// Find documents with a specific condition
db.students.find({ city: "Mumbai" });
// Find documents with age greater than 20
db.students.find({ age: { $gt: 20 } });
// Find one document
db.students.findOne({ name: "Priya Sharma" });
Use updateOne()
, updateMany()
, or replaceOne()
to modify data.
// Update one document
db.students.updateOne(
{ name: "Rajesh Kumar" },
{ $set: { age: 21 } }
);
// Update multiple documents
db.students.updateMany(
{ city: "Mumbai" },
{ $set: { status: "Active" } }
);
// Replace a document
db.students.replaceOne(
{ name: "Amit Singh" },
{ name: "Amit Singh", age: 23, city: "Hyderabad" }
);
Use deleteOne()
or deleteMany()
to remove data.
// Delete one document
db.students.deleteOne({ name: "Priya Sharma" });
// Delete multiple documents
db.students.deleteMany({ city: "Mumbai" });
Aggregation allows processing and analyzing data. Letโs explore with examples:
Count the number of students by city:
db.students.aggregate([
{ $group: { _id: "$city", count: { $sum: 1 } } }
]);
Calculate the average age of students:
db.students.aggregate([
{ $group: { _id: null, avgAge: { $avg: "$age" } } }
]);
Sort students by age in descending order:
db.students.aggregate([
{ $sort: { age: -1 } }
]);
To sort students by age in ascending order, use the $sort
stage in the aggregation pipeline with age: 1
(where 1
indicates ascending order):
db.students.aggregate([
{ $sort: { age: 1 } }
]);
This will return the list of students sorted from the youngest to the oldest. If you are not using aggregation, you can achieve the same with a simple find()
and sort()
:
db.students.find().sort({ age: 1 });
show dbs;
use school;
show collections;
db.students.drop();
MongoDBโs flexibility and simplicity make it a great choice for beginners and professionals alike. Start by mastering basic CRUD operations, then explore the powerful Aggregation Framework for advanced queries.๐
https://www.infoq.com/articles/Starting-With-MongoDB/
https://www.w3schools.com/mongodb/
https://www.geeksforgeeks.org/mongodb-tutorial/
https://www.mongodb.com/resources/products/fundamentals/basics