Data Science Practical No 02
Data Science Practical No 02
➢ updateMany():-
> db.student.find()
{ "_id" : ObjectId("63bd197e2bfbb5e9683ccef7"), "RollNo" : 2, "name" : "Rohan",
"email" : "ramparab23@gmail.com" }
{ "_id" : ObjectId("63c0ffffbcf4337ea53afd57"), "RollNo" : 4, "name" : "Nilesh Metar",
"email" : "nilesh12@gmail.com", "address" : "Malvan" }
{ "_id" : ObjectId("63c10050bcf4337ea53afd58"), "RollNo" : 5, "name" : "Sanvi",
"email" : "sanvi@gmail.com", "address" : "Kudal" }
{ "_id" : ObjectId("63c1021bbcf4337ea53afd5b"), "stud_record" : [ { "RollNo" : 6,
"name" : "Yash Sawant", "email" : "yash@gmail.com" }, { "RollNo" : 7, "name" :
"Manish Panday", "address" : "Malvan" } ] }
{ "_id" : ObjectId("63c648d8f78f21ea3dcb6f2c"), "RollNo" : 1, "name" : "Rohan",
"email" : "satyavanmestry360@gmail.com" }
> db.student.updateMany({RollNo:{$gt:1}},{address:"Kankavli"})
uncaught exception: Error: the update operation document must contain atomic
operators :
DBCollection.prototype.updateMany@src/mongo/shell/crud_api.js:655:19
@(shell):1:1
> db.student.updateMany({RollNo:{$gt:1}},{$set:{address:"Kankavli"}})
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.student.find()
{ "_id" : ObjectId("63bd197e2bfbb5e9683ccef7"), "RollNo" : 2, "name" : "Rohan",
"email" : "ramparab23@gmail.com", "address" : "Kankavli" }
{ "_id" : ObjectId("63c0ffffbcf4337ea53afd57"), "RollNo" : 4, "name" : "Nilesh Metar",
"email" : "nilesh12@gmail.com", "address" : "Kankavli" }
{ "_id" : ObjectId("63c10050bcf4337ea53afd58"), "RollNo" : 5, "name" : "Sanvi",
"email" : "sanvi@gmail.com", "address" : "Kankavli" }
{ "_id" : ObjectId("63c1021bbcf4337ea53afd5b"), "stud_record" : [ { "RollNo" : 6,
"name" : "Yash Sawant", "email" : "yash@gmail.com" }, { "RollNo" : 7, "name" :
"Manish Panday", "address" : "Malvan" } ] }
{ "_id" : ObjectId("63c648d8f78f21ea3dcb6f2c"), "RollNo" : 1, "name" : "Rohan",
"email" : "satyavanmestry360@gmail.com" }
>
• Without ObjectID:-
{ "_id" : ObjectId("63c1021bbcf4337ea53afd5b"), "RollNo" : 10, "name" : "Vaibhav",
"address" : "Vengurla" }
{ "_id" : ObjectId("63c648d8f78f21ea3dcb6f2c"), "RollNo" : 1, "name" : "Rohan",
"email" : "satyavanmestry360@gmail.com" }
> db.student.save({RollNo:10,name:"Gita",address:"Kudal"})
WriteResult({ "nInserted" : 1 })
> db.student.find()
{ "_id" : ObjectId("63bd197e2bfbb5e9683ccef7"), "RollNo" : 2, "name" : "Rohan",
"email" : "ramparab23@gmail.com", "address" : "Kankavli" }
{ "_id" : ObjectId("63c0ffffbcf4337ea53afd57"), "RollNo" : 4, "name" : "Nilesh Metar",
"email" : "nilesh12@gmail.com", "address" : "Kankavli" }
{ "_id" : ObjectId("63c10050bcf4337ea53afd58"), "RollNo" : 5, "name" : "Sanvi",
"email" : "sanvi@gmail.com", "address" : "Kankavli" }
{ "_id" : ObjectId("63c1021bbcf4337ea53afd5b"), "RollNo" : 10, "name" : "Vaibhav",
"address" : "Vengurla" }
{ "_id" : ObjectId("63c648d8f78f21ea3dcb6f2c"), "RollNo" : 1, "name" : "Rohan",
"email" : "satyavanmestry360@gmail.com" }
{ "_id" : ObjectId("63c64e20f78f21ea3dcb6f2d"), "RollNo" : 10, "name" : "Gita",
"address" : "Kudal" }
>
• skip():-
> db.student.find().skip(2)
{ "_id" : ObjectId("63c10050bcf4337ea53afd58"), "RollNo" : 5, "name" : "Sanvi",
"email" : "sanvi@gmail.com", "address" : "Kankavli" }
{ "_id" : ObjectId("63c1021bbcf4337ea53afd5b"), "RollNo" : 10, "name" : "Vaibhav",
"address" : "Vengurla" }
{ "_id" : ObjectId("63c648d8f78f21ea3dcb6f2c"), "RollNo" : 1, "name" : "Rohan",
"email" : "satyavanmestry360@gmail.com" }
{ "_id" : ObjectId("63c64e20f78f21ea3dcb6f2d"), "RollNo" : 10, "name" : "Gita",
"address" : "Kudal" }
>
➢ MongoDB Indexing:-
01)Ascending Order Index:-
> db.student.createIndex({name:1})
{
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"createdCollectionAutomatically" : false,
"ok" : 1
}
>
➢ Removing Index:-
> db.student.dropIndex({name:1})
{ "nIndexesWas" : 3, "ok" : 1 }
> db.student.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"address" : -1
},
"name" : "address_-1"
}
]
>