how to delete all documents in mongodb


How to delete all documents in mongodb

db.Collection_Name.remove({}) is use to delete all document in mongodb.

Let’s insert some data to delete all record

db.stud.save([
{
 "roll":101,
 "name":"Amar Kumar",
 "addr":"Hyderabad"   
},
{
 "roll":102,
 "name":"Vikash Kumar",
 "addr":"Hyderabad"   
},
{
 "roll":103,
 "name":"Vicky Kumar",
 "addr":"Pune"   
}]
)

Show Record

db.getCollection('stud').find({})
/* 1 */
{
    "_id" : ObjectId("56e3f68b24f2c2611ca9c296"),
    "roll" : 101,
    "name" : "Amar Kumar",
    "addr" : "Hyderabad"
}

/* 2 */
{
    "_id" : ObjectId("56e3f68b24f2c2611ca9c297"),
    "roll" : 102,
    "name" : "Vikash Kumar",
    "addr" : "Hyderabad"
}

/* 3 */
{
    "_id" : ObjectId("56e3f68b24f2c2611ca9c298"),
    "roll" : 103,
    "name" : "Vicky Kumar",
    "addr" : "Pune"
}

Delete All Record

db.stud.remove({})

Removed 3 record(s) in 2ms

Show Records

db.getCollection('stud').find({})

Please observe above there is no record.






Full-stack web and mobile app development company Contact Us : Skype: indore.webcoder Email: info@w3sparks.com Sales: +91 8085506229