How to delete documents from a collection in MongoDB?

MongoDBDatabaseBig Data Analytics

To delete the documents from a collection in MongoDB, you need to use remove() method. The syntax is as follows:

db.yourCollectionName.remove(yourDeleteValue);

Here, let us create a collection with some documents. The query is as follows:

>db.deleteDocuments.insert({"UserId":1,"UserName":"Bob","UserTechnicalSubject":"Introducti on to PL/SQL"});
WriteResult({ "nInserted" : 1 })

>db.deleteDocuments.insert({"UserId":2,"UserName":"Carol","UserTechnicalSubject":"Introduction to MongoDB"});
WriteResult({ "nInserted" : 1 })

>db.deleteDocuments.insert({"UserId":3,"UserName":"John","UserTechnicalSubject":"Introduction to MySQL"});
WriteResult({ "nInserted" : 1 })

>db.deleteDocuments.insert({"UserId":4,"UserName":"Maxwell","UserTechnicalSubject":"Introduction to SQL Server"});
WriteResult({ "nInserted" : 1 })

You can display all documents from the collection we created above with the help of find() command. The query is as follows:

> db.deleteDocuments.find().pretty();

The following is the output:

{
   "_id" : ObjectId("5c6aaa9e64f3d70fcc9147ff"),
   "UserId" : 1,
   "UserName" : "Bob",
   "UserTechnicalSubject" : "Introduction to PL/SQL"
}
{
   "_id" : ObjectId("5c6aaab464f3d70fcc914800"),
   "UserId" : 2,
   "UserName" : "Carol",
   "UserTechnicalSubject" : "Introduction to MongoDB"
}
{
   "_id" : ObjectId("5c6aaac764f3d70fcc914801"),
   "UserId" : 3,
   "UserName" : "John",
   "UserTechnicalSubject" : "Introduction to MySQL"
}
{
   "_id" : ObjectId("5c6aaadc64f3d70fcc914802"),
   "UserId" : 4,
   "UserName" : "Maxwell",
   "UserTechnicalSubject" : "Introduction to SQL Server"
}

Delete a single document

To delete a single document from a collection, you need to use remove() method. The query is as follows. Let’s say we are deleting document with “UserId:4”:

>db.deleteDocuments.remove({"UserId":4,"UserName":"Maxwell","UserTechnicalSubject":"Int
roduction to SQL Server"});
WriteResult({ "nRemoved" : 2 })

Now you can display all the documents with the help of find() command to verify that “UserId:4” document is deleted successfully or not. The query is as follows:

> db.deleteDocuments.find().pretty();

The following is the output:

{
   "_id" : ObjectId("5c6aaa9e64f3d70fcc9147ff"),
   "UserId" : 1,
   "UserName" : "Bob",
   "UserTechnicalSubject" : "Introduction to PL/SQL"
}
{
   "_id" : ObjectId("5c6aaab464f3d70fcc914800"),
   "UserId" : 2,
   "UserName" : "Carol",
   "UserTechnicalSubject" : "Introduction to MongoDB"
}
{
   "_id" : ObjectId("5c6aaac764f3d70fcc914801"),
   "UserId" : 3,
   "UserName" : "John",
   "UserTechnicalSubject" : "Introduction to MySQL"
}

Delete all the documents

To delete all the documents, you need to use the following syntax:

db.yourCollectionName.remove({ });

The query is as follows:

> db.deleteDocuments.remove({});

The following is the output:

WriteResult({ "nRemoved" : 3 })

We have deleted all the documents from the collection ‘deleteDocuments’:

raja
Published on 18-Mar-2019 15:13:29
Advertisements