CRUD: Create One – Command Prompt
mongo “mongodb+srv://cluster****.azure.mongodb.net”b–username newuser -password *****
db
show dbs
use sqlauthoritynew
db.createCollection(“newusers”)
show collections
db.newusers.insertOne(
{
“DisplayName”:”Pinal Dave”,
“UserName”:”pinaldave”,
“job”:{
“Title”:”DBA”,
“Area”:”Database Performance Tuning”,
“isManager”:false},
“Programming Language”:[“T-SQL”,”JS”,”HTML”]
}
)
db.newusers.find( {} )
db.newusers.find( {} ).pretty()
Demo: CRUD: Create Many – Command Prompt
cls
db.newusers.insertMany(
[
{
“DisplayName”:”Pinal Dave”,
“UserName”:”pinaldave”,
“job”: {
“Title”:”DBA”,
“Area”:”Database Performance Tuning”,
“isManager”:”false”
},
“Programming Language”:[“T-SQL”,”JS”,”HTML”]
},
{
“DisplayName”:”Jason Brown”,
“UserName”:”jasonbrown”,
“job”: {
“Title”:”DBA”,
“Area”:”Database Performance Tuning”,
“isManager”:”true”
},
“Programming Language”:[“T-SQL”,”JS”,”HTML”]
},
{
“DisplayName”:”Mark Smith”,
“UserName”:”marksmith”,
“job”: {
“Title”:”DBA”,
“Area”:”Database Performance Tuning”,
“isManager”:”false”,
“YearsExp”:{“$numberInt”:”5″}
},
“Programming Language”:[“T-SQL”,”HTML”]
}
]
)
db.newusers.find( {} ).pretty()
CRUD Operations: Retrieving Objects
db
show dbs
use sample_mflix
show collections
db.movies.find({})
db.movies.find({}).pretty()
db.movies.find({runtime: 11}).pretty()
db.movies.find({runtime: 11}).pretty().limit(3)
db.movies.find({runtime: 11},{runtime:1, title:1,_id:0}).pretty().limit(3)
db.movies.find({runtime: 11},{runtime:1, title:1}).pretty().limit(3)
db.movies.find({runtime: 11},{runtime:1, title:1}).pretty().limit(5).sort({title: 1})
db.movies.find({runtime: 11},{runtime:1, title:1}).pretty().limit(5).sort({title: -1})
db.movies.find({runtime: 11},{runtime:1, title:1}).pretty().limit(5).sort({title: -1}).readConcern(“majority”)
db.movies.find({runtime: 11},{runtime:1, title:1}).pretty().limit(5).sort({title: -1}).readConcern(“linearizable”).maxTimeMS(10000)
CRUD Operations: Updating and Deleting Objects
use sample_mflix
CRUD: Update One – Command Prompt
db.movies.updateOne( {title :{$eq: “The Old Crocodile” }},{ $set: { “title”: “The New Crocodile” }} )
db.movies.find({runtime: 12},{runtime:1, title:1, year:1, _id:0}).pretty().limit(3).sort({title: -1})
db.movies.updateOne( {title :{$eq: “The New Crocodile” }},{ $set: { “title”: “The Oldest Crocodile”, “Year”: 2020 }} )
db.movies.find({runtime: 12},{runtime:1, title:1, year:1, _id:0}).pretty().limit(3).sort({title: -1})
db.movies.updateOne( {title :{$eq: “The Oldest Crocodile” }},{ $set: { “title”: “The New Crocodile”, “year”: 2020 }} )
db.movies.find({runtime: 12},{runtime:1, title:1, year:1, Year:1, _id:0}).pretty().limit(3).sort({title: -1})
CRUD: Update One – Command Prompt
db.movies.find({year: {$eq: 1988}} ).count()
db.movies.find({year: {$eq: 2025}} ).count()
db.movies.updateMany({year: {$eq: 1988}}, { $set: { “year”: 2025 }})
db.movies.updateMany({year: {$eq: 1988}}, { $set: { “year”: 2025 }},{upsert:true})
db.movies.updateMany({year: {$eq: 1988}}, { $set: { “title”:”MySuperFunnyTitle”,”awards.wins”:9 }},{upsert:true})
db.movies.find({year: {$eq: 1988}} ).pretty()
db.movies.updateMany({runtime: {$eq: 1122}}, { $set: { “title”: “MySuperFunnyTitle”,”Year”: 2020, “awards.wins”:9 }},{upsert:true, w:”majority”, wtimeout:1000})
db.movies.find({runtime: 1122}).pretty()
db.movies.replaceOne({runtime: {$eq: 1122}}, { runtime:1122, “NoTitle”: “ReplaceOneExample”, “NewYear”: 2020, “awards.losts”: 5}
Demo: CRUD: Delete – Command Prompt
db.movies.find({runtime: 25}).count()
db.movies.deleteOne( {runtime: 25})
db.movies.deleteMany( {runtime: 25})
db.movies.find({runtime: 25}).count()
db.movies.find({runtime: 35}).count()
db.movies.remove({runtime: 35}, true )
db.movies.remove({runtime: 35} )
db.movies.remove({})
show collections
show dbs
db.foo.save({_id:1, x:10})
show collections
Set Operator
db.a.save({_id:1, x:10})
db.a.find()
db.a.update({_id:1},{$set:{y:3}})
db.a.update({_id:1},{$inc:{x:1}})
db.a.find()
Unset Operator
db.a.update({_id:1},{$unset:{y:”}})
Rename Operator
db.a.save({_id:1, Naem:’bob’})
db.a.update({_id:1},{$rename:{‘Naem’: ‘Name’}})
Push Operator
db.a.save({_id:2})
db.a.update({_id:2},{$push:{things: ‘One’}})
db.a.update({_id:2},{$push:{things: ‘Two’}})
db.a.update({_id:2},{$push:{things: ‘Three’}})
db.a.update({_id:2},{$addToSet:{things: ‘four’}})
Pull Operator
db.a.update({_id:2},{$push:{things: ‘Three’}})
db.a.update({_id:2},{$pull:{things: ‘Three’}})
Pop Operator
#Remove Last field
db.a.update({_id:2},{$pop:{things: 1 }})
#Remove First Field
db.a.update({_id:2},{$pop:{things: -1 }})
Multi Update
# Updates only 1st record
db.a.update({},{$push:{things: 4 }});
#Updates all records
db.a.update({},{$push:{things: 4 }},{multi:true});
#Updates all records that have 2 in the array
db.a.update({things:2} ,{$push:{things: 42}},{multi:true});
FInd
# Return where ID = 1 and show 1 field
db.a.find({_id:1},{_id:1})
db.a.find({_id: {$gt:2} }, {_id:1})
db.a.find({_id: {$lt:2} }, {_id:1})
db.a.find({_id: {$lte:2} }, {_id:1})
db.a.find({_id: {$gte:2} }, {_id:1})
#Range
db.a.find({_id: {$gte:2,$lt:4} }, {_id:1})
db.a.find({_id: {$not: {$gt:2}}}, {_id:1})
db.a.find({_id: {$in: [1,3] } }, {_id:1})
db.a.find({_id: {$nin: [1,3] } }, {_id:1})
#Sort Asc
db.a.find().sort({_id:1})
#Sort Desc
db.a.find().sort({_id:-1})
Indexes
#Show Indexes
db.system.indexes.find({ns:’a.things’},{key:1})
#Explain
db.a.find({things: 4}).explain()
Create Index
db.a.ensureIndex({things:1})
Drop Index
db.a.dropIndex(“things_1”)