75 views
asked in MongoDB by
MongoDB CRUD Operations Step by Step Guide

1 Answer

answered by

inchirags@gmail.com   Chirag's MongoDB DBA Tutorial         https://www.chirags.in

*****************************************************************************************

*MongoDB CRUD Operations Step by Step Guide*

*****************************************************************************************

YouTube Video link:

https://youtu.be/K3QIUMmhX7Q

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.

Most popular tags

postgresql laravel replication ha postgresql laravel-10 mongodb ubuntu 24.04 lts mongodb database mongodb tutorial streaming-replication mysql laravel-11 database laravel postgresql backup database mysql php technlogy asp.net asp.net c# mysql master slave replication centos linux laravel sql server schedule backup autobackup postgresql django python user and admin registration user and admin login multiauth zabbix 7 how to install graylog on ubuntu 24.04 lts | step-by-step asp.net core mvc .net mvc network upload c# ssl integration sql server on ubuntu 22.04 lts mssql server ms sql server sql server user access in postgres mysql password change cent os linux laravel login register logout replica php in iis php with iis php tutorial chirags php tutorials chirags php tutorial chirags tutorial laravel 11 gaurds laravel 11 guards mongodb sharding metabase business analytics metabase ubuntu 24.04 koha 24.05 postgresql 16 to postgresql 17 postgresql migration letsencrypt mongodb crud rocky linux laravel custom captcha laravel 11 captcha laravel captcha mongo dll php.ini debian 12 nginx apache nextcloud gitea in ubuntu git gitea npm error node js mysql ndb cluster mysql cluster ssl oracle login register logout in python debian windows shell batch file bat file time stamp date time shopping cart in laravel centos rhel swap memeory rhel 5.5 access configuration in postgresql hba configuration laravel multiple database configuration state city country dropdown live photo upload webcam captcha in laravel
...