Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Mongo DB

Data Types in MongoDB


• Strings
• Boolean
• Integer
• Double
• Arrays
• ObjectId
• Data
DataBase Commands
View all Databases
➢ Command: Show dbs
Create a new or switch databases
➢ To use particular database
➢ Command: Use dbName
View current Databases
➢ Command: db
To delete Database
➢ To delete current using database
➢ Command :db.dropDatabase()
Collections in Mongodb
➢ Database in mongodb contains Collection and it contains documents and fields
.where as in mysql it contains tables and it contains rows and columns.

To see the collections in current Database

MARUTHI CHARAN TEJ 1


➢ Command: Show collections
To create collection in current database
➢ Command: Db.createCollection(‘comments’)
➢ Comments is the name of the collection
To Drop a collection named ‘comments’
➢ Command : db.comments.drop()

Crud operations in Mongodb


• C→create
• R→Read
• U→Update
• D→delete

Row (document ) commands


To insert one Row
db.developer.insertOne({
‘name’:‘charantej’,
‘lang’:‘MongoDb’,
‘member_since’:5
})
To insert Many Rows
db.developer.insertMany([{
'name':'maruthi',
'lang':'web devlopment',
'member_since':5
},
{
'name':'charantej',
'lang':'MongoDb',
'member_since':6
},
{
'name':'jos',
'lang':'java',
'member_since':5
}])

Search in a MogoDb Database


db.developer.find({lang:'MongoDb',})

MARUTHI CHARAN TEJ 2


limit the number of rows in output
db.developer.find().limit(2)
To count the number of rows in the output
db.developer.find().count()

To update a row
• If the row is matched in the document

db.developer.updateOne({'name':'virat'},
{$set:{
'name':'priya',
'lang':'javascript',
'member_since':5
}},{upsert:true})
-------------------------------------------------
→if the row is matched the upsertedcount=0
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}

• If the row is not matched in document


db.developer.updateOne({'name':'virat'},
{$set:{
'name':'priya',
'lang':'javascript',
'member_since':5
}},{upsert:true})
------------------------------------------------------
→if the row is not matched upsertedcount=1
{
acknowledged: true,
insertedId: ObjectId("64eae96e11e55290685301b2"),
matchedCount: 0,
modifiedCount: 0,
upsertedCount: 1
}

Note

MARUTHI CHARAN TEJ 3


➢ If the upsertedcount is 1 that means the row is not matched and new row instead of
updating it.
➢ If the upsertedcount is 0 that means the row is matched it will update the row to the
matched data.

Mongodb Increment operator


➢ Is used the increment particular value of a specific row
db.developer.update({name:'priya'},
{$inc:{
member_since:3
}}
---------------------------------------------
DeprecationWarning: Collection.update() is deprecated. Use
updateOne, updateMany, or bulkWrite.
-------------------------------------------------
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}

Rename operator
➢ To change name of the row
db.developer.updateMany({name:'priya'},
{$rename:{
member:'member_since'
}}
)
----------------------------------------------------------
{
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0

To Delete row
➢ To delete a particular row in the document.

MARUTHI CHARAN TEJ 4


db.developer.remove({name:'priya'})

To show all Rows in a collection/find the rows in the collection


db.developer.find()
db.developer.find().pretty()
-------------------------------------------------------------
Output:
[
{
_id: ObjectId("64eae25d2a2c9bee1fe45bf7"),
name: 'maruthi',
lang: 'web devlopment',
member_since: 5
},
{
_id: ObjectId("64eae25d2a2c9bee1fe45bf8"),
name: 'charantej',
lang: 'MongoDb',
member_since: 6
}
]
To find a particular row in a collection
db.developer.findOne({name:'charantej'})

MARUTHI CHARAN TEJ 5

You might also like