inchirags@gmail.com Chirag's MongoDB DBA Tutorial https://www.chirags.in
*****************************************************************************************
*MongoDB CRUD Operations Step by Step Guide*
*****************************************************************************************
YouTube Video link:
MongoDB Database server:
Server IP: 192.168.224.xxx
MongoDB provides CRUD (Create, Read, Update, Delete) operations to manage data. Below are detailed steps with examples for each operation.
Access MongoDB Datababase in command line:
mongosh --host 127.0.0.1 --port 27017 -u "admin" -p "admin@123" --authenticationDatabase "admin"
--or--
mongosh --host 127.0.0.1 --port 27017 -u "admin" -p "admin@123"
Grant Appropriate Role: If the user does not have sufficient permissions, you'll need to grant them the necessary role. For example, to grant the readWrite role on the "chirags" database, use the following command:
db.updateUser("admin", { roles: [{ role: "readWrite", db: "chirags" }] })
1. Create (Insert)
Used to add new documents to a MongoDB collection.
Example: Insert a single document
First select database:
test> use chirags
switched to db chirags
chirags>
Now Insert data into chirags database:
chirags> db.users.insertOne({
name: "Chirag Mahto",
age: 30,
city: "Ranchi"
})
Example: Insert multiple documents
chirags> db.users.insertMany([
{ name: "Purab", age: 35, city: "Ranchi" },
{ name: "Sanju", age: 30, city: "Jamshedpur" }
])
Explanation:
insertOne() is used for inserting a single document.
insertMany() allows insertion of multiple documents in an array.
2. Read (Query)
Used to fetch data from a collection.
Example: Find all documents
chirags> db.users.find()
Example: Find documents with specific criteria
chirags> db.users.find({ age: { $gt: 30 } })
Example: Find only one document
chirags> db.users.findOne({ name: "Purab" })
Explanation:
find() retrieves all matching documents. You can add filters like { age: { $gt: 35 } } to query based on conditions (e.g., age greater than 35).
findOne() returns the first matching document.
Advanced Query Example: Query with Projection
chirags> db.users.find({ age: { $gt: 33 } }, { name: 1, city: 1, _id: 0 })
Explanation: The second argument specifies the fields to return. { name: 1, city: 1, _id: 0 } shows only name and city while excluding _id.
3. Update
Used to modify existing documents in a collection.
Example: Update a single document
chirags> db.users.updateOne(
{ name: "Chirag Mahto" }, // filter
{ $set: { age: 33 } } // update operation
)
Example: Update multiple documents
chirags> db.users.updateMany(
{ city: "Ranchi" }, // filter
{ $set: { city: "Bokaro" } } // update operation
)
Example: Replace a document
chirags> db.users.replaceOne(
{ name: "Chirag Mahto" }, // filter
{ name: "Chirag", age: 36, city: "Dhanbad" } // new document
)
Explanation:
updateOne() updates the first document that matches the filter.
updateMany() updates all documents that match the filter.
replaceOne() replaces an entire document with a new one.
Example: Update with Operators
chirags> db.users.updateOne(
{ name: "Chirag" },
{ $inc: { age: 1 }, $set: { city: "myCity" } }
)
Explanation: $inc increments the age field by 1, and $set updates the city field.
4. Delete
Used to remove documents from a collection.
Example: Delete a single document
chirags> db.users.deleteOne({ name: "Chirag" })
Example: Delete multiple documents
chirags> db.users.deleteMany({ age: { $lt: 30 } })
Explanation:
deleteOne() deletes the first matching document.
deleteMany() deletes all documents that match the filter.
Summary of Common MongoDB Operators
$gt, $lt, $gte, $lte: Greater than, Less than, Greater than or equal to, Less than or equal to.
$set: Set a value for a field.
$inc: Increment a field's value.
$and, $or: Logical AND, OR operations for multiple conditions.
For any doubts and query, please write on YouTube video comments section.
Note : Flow the Process shown in video.
Please, Subscribe and like for more videos:
https://youtube.com/@chiragstutorial
Don't forget to, Follow, Like, Share &, Comment
Thanks & Regards,
Chitt Ranjan Mahto "Chirag"
_________________________________________________________________________________________
Note: All scripts used in this demo will be available in our website.
Link will be available in description.