MongoDB query to remove entire data from a collection


To remove, use remove() in MongoDB. Let us create a collection with documents −

> db.demo309.insertOne({ "details":[ { "Name":"Chris" }, { "Name":"David" }, { "Name":"Adam" } ] } );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e4eb71af8647eb59e562040")
}
> db.demo309.insertOne({ "details":[ { "Name":"David" }, { "Name":"Mike" }, { "Name":"Bob" } ] } );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e4eb7cbf8647eb59e562041")
}

Display all documents from a collection with the help of find() method −

> db.demo309.find();

This will produce the following output −

{ "_id" : ObjectId("5e4eb71af8647eb59e562040"), "details" : [ { "Name" : "Chris" }, { "Name" : "David" }, { "Name" : "Adam" } ] }
{ "_id" : ObjectId("5e4eb7cbf8647eb59e562041"), "details" : [ { "Name" : "David" }, { "Name" : "Mike" }, { "Name" : "Bob" } ] }

Following is the query to remove data from big collection −

> db.demo309.remove({});

This will produce the following output −

WriteResult({ "nRemoved" : 2 })

Updated on: 01-Apr-2020

141 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements