Update Operation In MonGoDB Using Robo 3T

In this article, we will learn how to Update 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 and Delete operations in MongoDB.

In this article, we will learn how to update the data from MongoDB Using Robo 3T. We will learn from the basics because I have written this article focusing on beginners. In the previous article, we have learned the Insert and Delete operations in MongoDB. This article will give some basic understanding of how we can use a Different Method of MongoDB to perform Crud Operation. You can check that from here,
You can check how to set up the MongoDB environment from here

Update Method in MongoDB

  1. UpdateOne()- Using this function, we can update a single data to match the search criteria. Even multiple data values can match the search criteria.
  2. Updatemany()- Using this function we can update all the data that matches specified criteria.
  3. ReplaceOne: Using this Function we can replace a single data that matches search criteria. Even multiple data values can match the search criteria.
  4. Save()- the function replaces the data with the new data that passes in save()
  5. FindoneandUpdate()
  6. FindoneandReplace
Step 1
 
Open Visual Studio, create a new console application and rename it as UpdateOperation.
 
Update Operation In MonGoDB Using Robo 3T

Step 2

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

Step 3

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

Step 4

Now, we add a connection string.
  1. var connectionString = "mongodb://localhost";    
  2. var Client = new MongoClient(connectionString);    
We want to Update the data from Employeedetails Table from Employee Database. We will try to find the data using the Name field and based on the name we can update the city.
 
Step 5
 
Get the database and collection in the database. Now, we can find the data based on Name and Update City.
  1. var DB = Client.GetDatabase("Employee");    
  2. var collection = DB.GetCollection<BsonDocument>("EmployeeDetails");   

Step 6

Now, we find the data based on Name and Update City.
  1. var UpdateRecored = collection.FindOneAndUpdate(  
  2.                            Builders<BsonDocument>.Filter.Eq("Name""saqwwww"),  
  3.                             Builders<BsonDocument>.Update.Set("City""Hyderabad")  
Complete Code for Updating Data,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using MongoDB.Bson;  
  7. using MongoDB.Driver;  
  8.   
  9. namespace UpdateOpration  
  10. {  
  11.     class Program  
  12.     {  
  13.         static void Main(string[] args)  
  14.         {  
  15.             var connectionString = "mongodb://localhost";  
  16.             var Client = new MongoClient(connectionString);  
  17.             var DB = Client.GetDatabase("Employee");  
  18.             var collection = DB.GetCollection<BsonDocument>("EmployeeDetails");  
  19.             var UpdateRecored = collection.FindOneAndUpdate(  
  20.                            Builders<BsonDocument>.Filter.Eq("Name""saqwwww"),  
  21.                             Builders<BsonDocument>.Update.Set("City""Hyderabad")  
  22.   
  23.                            );  
  24.             Console.WriteLine("Update Data");  
  25.             Console.ReadLine();  
  26.         }  
  27.     }  
  28. }  

Now, run the program and check the table. We successfully updated the city based on Name,

Update Operation In MonGoDB Using Robo 3T 

Summary

In this article, we have learned how we can Update data from a database. We also learned about different update methods. In the next article we will learn Crud Operation in ASP.NET MVC Using MongoDB.