MongoDB Topics
Day 28:
- Introduction to MongoDB
- Setting up MongoDB
- CRUD operations with MongoDB
Introduction to MongoDB:
To read:
show databases
show collections
Create collection:
use HemaDatabase
db.createCollection('HemaCodingSchool')
db.HemaCodingSchool.insertOne({ name: 'Mahesh', role: 'Student'})
-------------------------------------------
Create:
db.HemaCodingSchool.insertOne(
{ name: 'Mahesh', role: 'Student' }
)
-------------------------------------------
Read:
db.HemaCodingSchool.find({})
-------------------------------------------
Update:
db.HemaCodingSchool.updateOne(
{ name: 'Mahesh'}, {$set:{ role: 'Emloyee'}}
)
-------------------------------------------
Delete:
db.HemaCodingSchool.deleteOne({name: 'Mahesh'})
==============================================
Create Many:
db.HemaCodingSchool.insertMany([
{ name: 'Hema', role: 'Organizer' },
{ name: 'Mahesh', role: 'Employee' },
{ name: 'Maruthi', role: 'Student' }
])
------------------------------------------------------
Update Many:
db.HemaCodingSchool.updateMany(
{ name: { $in: ['Hema', 'Mahesh', 'Maruthi'] } },
{ $set: { role: 'NewRole' } }
)
---------------------------------------------
Delete Many:
db.HemaCodingSchool.deleteMany(
{ name: { $in: ['Hema', 'Mahesh', 'Maruthi'] } }
)
Interview Questions:
1. What is a Collection in MongoDB?
2. How do you insert a document in MongoDB?
3. Explain the $set operator in MongoDB?
No comments:
Post a Comment