0% found this document useful (0 votes)
51 views12 pages

Data Science Practical No 02

This document discusses various methods for modifying documents in a MongoDB collection called "student". These include removing collections, documents, and multiple documents using queries; updating documents by changing field values; and using findOneAndUpdate() to modify the first matching document.

Uploaded by

Satyavan Mestry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views12 pages

Data Science Practical No 02

This document discusses various methods for modifying documents in a MongoDB collection called "student". These include removing collections, documents, and multiple documents using queries; updating documents by changing field values; and using findOneAndUpdate() to modify the first matching document.

Uploaded by

Satyavan Mestry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

❖ Practical No:02)Practical of Data collection, Data curation, and

management for Large-scale Data systems (such as MongoDB).


➢ Remove Collections:
> show collections
student
studentdata
studentdata2
> db.studentdata2.drop()
true
> show collections
student
studentdata
>

➢ Remove the document from collection.


> db.student.remove({RollNo:1})
WriteResult({ "nRemoved" : 1 })
> db.student.find()
{ "_id" : ObjectId("63bd197e2bfbb5e9683ccef7"), "RollNo" : 2, "name" : "Ram Parab",
"email" : "ramparab23@gmail.com" }
{ "_id" : ObjectId("63c0f987bcf4337ea53afd56"), "RollNo" : 3, "name" : "Kajal Hatle",
"email" : "kajal@gmail.com" }
{ "_id" : ObjectId("63c0ffffbcf4337ea53afd57"), "RollNo" : 4, "name" : "Nilesh Metar",
"email" : "nilesh12@gmail.com", "address" : "Ramgad" }
{ "_id" : ObjectId("63c10050bcf4337ea53afd58"), "RollNo" : 5, "name" : "Sanvi",
"email" : "sanvi@gmail.com", "address" : "Kudal" }
{ "_id" : ObjectId("63c10209bcf4337ea53afd59"), "RollNo" : 6, "name" : "Yash
Sawant", "email" : "yash@gmail.com" }
{ "_id" : ObjectId("63c10209bcf4337ea53afd5a"), "RollNo" : 7, "name" : "Manish
Panday", "address" : "Malvan" }
{ "_id" : ObjectId("63c1021bbcf4337ea53afd5b"), "stud_record" : [ { "RollNo" : 6,
"name" : "Yash Sawant", "email" : "yash@gmail.com" }, { "RollNo" : 7, "name" :
"Manish Panday", "address" : "Malvan" } ] }
{ "_id" : ObjectId("63c1031dbcf4337ea53afd5c"), "RollNo" : 6, "name" : "Yash
Sawant", "email" : "yash@gmail.com" }
{ "_id" : ObjectId("63c1031dbcf4337ea53afd5d"), "RollNo" : 7, "name" : "Manish
Panday", "address" : "Malvan" }
>
➢ Remove multiple documents:
> db.student.remove({RollNo:{$gt:6}})
WriteResult({ "nRemoved" : 2 })
> db.student.find()
{ "_id" : ObjectId("63bd197e2bfbb5e9683ccef7"), "RollNo" : 2, "name" : "Ram Parab",
"email" : "ramparab23@gmail.com" }
{ "_id" : ObjectId("63c0f987bcf4337ea53afd56"), "RollNo" : 3, "name" : "Kajal Hatle",
"email" : "kajal@gmail.com" }
{ "_id" : ObjectId("63c0ffffbcf4337ea53afd57"), "RollNo" : 4, "name" : "Nilesh Metar",
"email" : "nilesh12@gmail.com", "address" : "Ramgad" }
{ "_id" : ObjectId("63c10050bcf4337ea53afd58"), "RollNo" : 5, "name" : "Sanvi",
"email" : "sanvi@gmail.com", "address" : "Kudal" }
{ "_id" : ObjectId("63c10209bcf4337ea53afd59"), "RollNo" : 6, "name" : "Yash
Sawant", "email" : "yash@gmail.com" }
{ "_id" : ObjectId("63c1021bbcf4337ea53afd5b"), "stud_record" : [ { "RollNo" : 6,
"name" : "Yash Sawant", "email" : "yash@gmail.com" }, { "RollNo" : 7, "name" :
"Manish Panday", "address" : "Malvan" } ] }
{ "_id" : ObjectId("63c1031dbcf4337ea53afd5c"), "RollNo" : 6, "name" : "Yash
Sawant", "email" : "yash@gmail.com" }
>

➢ Use of findOneAndDelete method:- Remove first matching document


from the collection.
> db.student.findOneAndDelete({RollNo:{$gt:2}})
{
"_id" : ObjectId("63c0f987bcf4337ea53afd56"),
"RollNo" : 3,
"name" : "Kajal Hatle",
"email" : "kajal@gmail.com"
}
> db.student.find()
{ "_id" : ObjectId("63bd197e2bfbb5e9683ccef7"), "RollNo" : 2, "name" : "Ram Parab",
"email" : "ramparab23@gmail.com" }
{ "_id" : ObjectId("63c0ffffbcf4337ea53afd57"), "RollNo" : 4, "name" : "Nilesh Metar",
"email" : "nilesh12@gmail.com", "address" : "Ramgad" }
{ "_id" : ObjectId("63c10050bcf4337ea53afd58"), "RollNo" : 5, "name" : "Sanvi",
"email" : "sanvi@gmail.com", "address" : "Kudal" }
{ "_id" : ObjectId("63c10209bcf4337ea53afd59"), "RollNo" : 6, "name" : "Yash
Sawant", "email" : "yash@gmail.com" }
{ "_id" : ObjectId("63c1021bbcf4337ea53afd5b"), "stud_record" : [ { "RollNo" : 6,
"name" : "Yash Sawant", "email" : "yash@gmail.com" }, { "RollNo" : 7, "name" :
"Manish Panday", "address" : "Malvan" } ] }
{ "_id" : ObjectId("63c1031dbcf4337ea53afd5c"), "RollNo" : 6, "name" : "Yash
Sawant", "email" : "yash@gmail.com" }
>

➢ Use of deleteMany():-Delete all matching documents from the


collection.
> db.student.findOneAndDelete({RollNo:{$gt:2}})
{
"_id" : ObjectId("63c0f987bcf4337ea53afd56"),
"RollNo" : 3,
"name" : "Kajal Hatle",
"email" : "kajal@gmail.com"
}
> db.student.find()
{ "_id" : ObjectId("63bd197e2bfbb5e9683ccef7"), "RollNo" : 2, "name" : "Ram Parab",
"email" : "ramparab23@gmail.com" }
{ "_id" : ObjectId("63c0ffffbcf4337ea53afd57"), "RollNo" : 4, "name" : "Nilesh Metar",
"email" : "nilesh12@gmail.com", "address" : "Ramgad" }
{ "_id" : ObjectId("63c10050bcf4337ea53afd58"), "RollNo" : 5, "name" : "Sanvi",
"email" : "sanvi@gmail.com", "address" : "Kudal" }
{ "_id" : ObjectId("63c10209bcf4337ea53afd59"), "RollNo" : 6, "name" : "Yash
Sawant", "email" : "yash@gmail.com" }
{ "_id" : ObjectId("63c1021bbcf4337ea53afd5b"), "stud_record" : [ { "RollNo" : 6,
"name" : "Yash Sawant", "email" : "yash@gmail.com" }, { "RollNo" : 7, "name" :
"Manish Panday", "address" : "Malvan" } ] }
{ "_id" : ObjectId("63c1031dbcf4337ea53afd5c"), "RollNo" : 6, "name" : "Yash
Sawant", "email" : "yash@gmail.com" }
>

➢ Updating documents in collections:-


> db.student.findOneAndDelete({RollNo:{$gt:2}})
{
"_id" : ObjectId("63c0f987bcf4337ea53afd56"),
"RollNo" : 3,
"name" : "Kajal Hatle",
"email" : "kajal@gmail.com"
}
> db.student.find()
{ "_id" : ObjectId("63bd197e2bfbb5e9683ccef7"), "RollNo" : 2, "name" : "Ram Parab",
"email" : "ramparab23@gmail.com" }
{ "_id" : ObjectId("63c0ffffbcf4337ea53afd57"), "RollNo" : 4, "name" : "Nilesh Metar",
"email" : "nilesh12@gmail.com", "address" : "Ramgad" }
{ "_id" : ObjectId("63c10050bcf4337ea53afd58"), "RollNo" : 5, "name" : "Sanvi",
"email" : "sanvi@gmail.com", "address" : "Kudal" }
{ "_id" : ObjectId("63c10209bcf4337ea53afd59"), "RollNo" : 6, "name" : "Yash
Sawant", "email" : "yash@gmail.com" }
{ "_id" : ObjectId("63c1021bbcf4337ea53afd5b"), "stud_record" : [ { "RollNo" : 6,
"name" : "Yash Sawant", "email" : "yash@gmail.com" }, { "RollNo" : 7, "name" :
"Manish Panday", "address" : "Malvan" } ] }
{ "_id" : ObjectId("63c1031dbcf4337ea53afd5c"), "RollNo" : 6, "name" : "Yash
Sawant", "email" : "yash@gmail.com" }
>

➢ Updating all matching records:-


> db.student.find()
{ "_id" : ObjectId("63bd197e2bfbb5e9683ccef7"), "RollNo" : 2, "name" : "Satyavan",
"email" : "ramparab23@gmail.com" }
{ "_id" : ObjectId("63c0ffffbcf4337ea53afd57"), "RollNo" : 4, "name" : "Nilesh Metar",
"email" : "nilesh12@gmail.com", "address" : "Ramgad" }
{ "_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" : "Satyavan",
"email" : "satyavanmestry360@gmail.com" }
> db.student.update({name:"Satyavan"},{$set:{name:"Rohan"}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> 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" : "Ramgad" }
{ "_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" }

➢ Save:-Used to replace document with complete new document.


➢ findOneAndUpdate():-Update first matching document.
> 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" : "Ramgad" }
{ "_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.findOneAndUpdate({RollNo:{$gt:2}},{$set:{address:"Malvan"}})
{
"_id" : ObjectId("63c0ffffbcf4337ea53afd57"),
"RollNo" : 4,
"name" : "Nilesh Metar",
"email" : "nilesh12@gmail.com",
"address" : "Ramgad"
}
> 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" }
>

➢ 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" }
>

➢ Updating documents using save method:


• With ObjectID:-
>db.student.save({_id:ObjectId("63c1021bbcf4337ea53afd5b"),RollNo:10,name:"Vaib
hav",address:"Vengurla"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 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" }
>

• 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" }
>

➢ Sorting the document:-


01)Descending order:
> db.student.find({},{_id:0,RollNo:1,name:1}).sort({RollNo:-1})
{ "RollNo" : 10, "name" : "Vaibhav" }
{ "RollNo" : 10, "name" : "Gita" }
{ "RollNo" : 5, "name" : "Sanvi" }
{ "RollNo" : 4, "name" : "Nilesh Metar" }
{ "RollNo" : 2, "name" : "Rohan" }
{ "RollNo" : 1, "name" : "Rohan" }
>

02) Ascending order:


> db.student.find().sort({})
{ "_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" }
>

➢ Sort column that don’t want to project:-


> db.student.find({},{_id:0,RollNo:0}).sort({RollNo:1})
{ "name" : "Rohan", "email" : "satyavanmestry360@gmail.com" }
{ "name" : "Rohan", "email" : "ramparab23@gmail.com", "address" : "Kankavli" }
{ "name" : "Nilesh Metar", "email" : "nilesh12@gmail.com", "address" : "Kankavli" }
{ "name" : "Sanvi", "email" : "sanvi@gmail.com", "address" : "Kankavli" }
{ "name" : "Vaibhav", "address" : "Vengurla" }
{ "name" : "Gita", "address" : "Kudal" }
>

➢ Use of limit() and skip():-


• limit():-
> db.student.find().limit(2)
{ "_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" }
>

• 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" }
>

➢ Skip 2 records and display only one of remaining:-


> db.student.find().skip(2).limit(1)
{ "_id" : ObjectId("63c10050bcf4337ea53afd58"), "RollNo" : 5, "name" : "Sanvi",
"email" : "sanvi@gmail.com", "address" : "Kankavli" }
> db.student.find().limit(1).skip(2)
{ "_id" : ObjectId("63c10050bcf4337ea53afd58"), "RollNo" : 5, "name" : "Sanvi",
"email" : "sanvi@gmail.com", "address" : "Kankavli" }
>

➢ MongoDB Indexing:-
01)Ascending Order Index:-
> db.student.createIndex({name:1})
{
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"createdCollectionAutomatically" : false,
"ok" : 1
}
>

02)Descending Order Index:-


> db.student.createIndex({address:-1})
{
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"createdCollectionAutomatically" : false,
"ok" : 1
}
>

➢ View all indexes:-


> db.student.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"name" : 1
},
"name" : "name_1"
},
{
"v" : 2,
"key" : {
"address" : -1
},
"name" : "address_-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"
}
]
>

➢ Drop all indexes from collection:-


> db.student.dropIndexes()
{
"nIndexesWas" : 2,
"msg" : "non-_id indexes dropped for collection",
"ok" : 1
}
> db.student.getIndexes()
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]
>

You might also like