Update Operation In MonGoDB Using Robo 3T

Insert 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 beginners. In the previous article, we 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 the 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.

using MongoDB.Driver;    
using MongoDB.Bson;    

Step 4. Now, we add a connection string.

var connectionString = "mongodb://localhost";    
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.

var DB = Client.GetDatabase("Employee");    
var collection = DB.GetCollection<BsonDocument>("EmployeeDetails");   

Step 6.Now, we find the data based on Name and Update City.

var UpdateRecored = collection.FindOneAndUpdate(  
Builders<BsonDocument>.Filter.Eq("Name", "saqwwww"),  
Builders<BsonDocument>.Update.Set("City", "Hyderabad")  

Complete Code for Updating Data,

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using MongoDB.Bson;  
using MongoDB.Driver;  
  
namespace UpdateOpration  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            var connectionString = "mongodb://localhost";  
            var Client = new MongoClient(connectionString);  
            var DB = Client.GetDatabase("Employee");  
            var collection = DB.GetCollection<BsonDocument>("EmployeeDetails");  
            var UpdateRecored = collection.FindOneAndUpdate(  
                           Builders<BsonDocument>.Filter.Eq("Name", "saqwwww"),  
                            Builders<BsonDocument>.Update.Set("City", "Hyderabad")  
  
                           );  
            Console.WriteLine("Update Data");  
            Console.ReadLine();  
        }  
    }  
}  

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.


Similar Articles