Delete Operation In MonGoDB Using Robo 3T

In this article, we will learn how to delete the data from MongoDB using Robo 3T. We will learn from the basics because I have written this article focusing on the beginners. In my previous article, we learned the Insert operations in MongoDB.

In this article, we will learn how to delete the data from MongoDB using Robo 3T. We will learn from the basics because I have written this article focusing on the beginners. In my previous article, we learned the Insert operations in MongoDB. You can check that from here.

Prerequisites

  • Visual Studio
  • MongoDB installed
  • Robo 3T

You can check how to set up the MongoDB environment from here.

Steps to delete the data from MongoDB Table (Collection).

Delete Method in MongoDB

  1. Remove() - Using this function, we can delete one or all data that matches the search criteria
  2. deleteone() - Using the deleteone() function, we can delete one document from the Table (Collection), even if multiple items match the criteria
  3. deletemany() - Using the deletemany() function, we can delete all the matching data
Step 1
 
Open Visual Studio, create a new console application, and rename it as DeleteOperaion.
 
Delete Operation in MonGoDB Using Robo 3T

Step 2

Add MongoDB Drivers for C# using NuGet Package Manager.
 
Delete Operation in MonGoDB Using Robo 3T 

Step 3

Add the Namespace.
  1. using MongoDB.Driver;  
  2. using MongoDB.Bson;  

Step 4

Now, add a connection string.
  1. var connectionString = "mongodb://localhost";  
  2. var Client = new MongoClient(connectionString);  

We want to delete the data from Employeedetails Table from Employee Database. We will try to find the data using the Name field and delete the data based on the name. 

Step 5
 
Get the database and collection in the database.
  1. var DB = Client.GetDatabase("Employee");  
  2. var collection = DB.GetCollection<BsonDocument>("EmployeeDetails");  

Step 6

Now, find the Data from Table based on the name field to search. Then, we used a filter and deleted the matching data from table.
  1. var DeleteRecored= collection.DeleteOne(  
  2. Builders<BsonDocument>.Filter.Eq("Name""Sanwar"));  

Now, check the complete code.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using MongoDB.Driver;  
  7. using MongoDB.Bson;  
  8. namespace DeleteOperation  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             var connectionString = "mongodb://localhost";  
  15.             var Client = new MongoClient(connectionString);  
  16.             var DB = Client.GetDatabase("Employee");  
  17.             var collection = DB.GetCollection<BsonDocument>("EmployeeDetails");  
  18.             var DeleteRecored= collection.DeleteOne(  
  19.                            Builders<BsonDocument>.Filter.Eq("Name""Sanwar"));  
  20.             Console.WriteLine("Delete Data");  
  21.             Console.ReadLine();  
  22.   
  23.         }  
  24.     }  
  25. }  

Before running the program, check the table data.

Delete Operation in MonGoDB Using Robo 3T 
 
Now, run the program and check the table. We search the data based on the Name field and delete the matched results from the table.
 
Delete Operation in MonGoDB Using Robo 3T 

Summary

In this article, we have learned how we can delete data from a database. We deleted the data from the database using a console application.