dotConnect for MongoDB Documentation
On this page
    Updating Data
    On this page

    You can update MongoDB data either by modifying the data returned by the MongoDBDataAdapter class and then calling its Update method, or by executing the corresponding DML statements (INSERT, DELETE, UPDATE) via MongoDBCommand.

    Here is an example that shows how to update MongoDB data using MongoDBDataAdapter. The document key is the _id field, so the update command uses it to identify the document to update.

    using Devart.Data.MongoDB;
    ...
    class Program
    {
    
        static void Main(string[] args) {
    
            const string connectionString = "Host=localhost;Database=sample;User Id=user;Password=pwd;License Key=***";
            const string sql = "SELECT _id, name FROM customers";
    
            using (MongoDBConnection connection = new MongoDBConnection(connectionString)) {
    
                connection.Open();
                DataTable table = new DataTable("customers");
    
                using (MongoDBCommand command = connection.CreateCommand()) {
    
                    command.CommandText = sql;
                    using (MongoDBDataAdapter adapter = new MongoDBDataAdapter(command)) {
    
                        adapter.Fill(table);
    
                        adapter.UpdateCommand = new MongoDBCommand("UPDATE customers SET name = :name WHERE _id = :id", connection);
    
                        adapter.UpdateCommand.Parameters.Add("id", DbType.String).SourceColumn = "_id";
                        adapter.UpdateCommand.Parameters["id"].SourceVersion = DataRowVersion.Original;
                        adapter.UpdateCommand.Parameters.Add("name", DbType.String).SourceColumn = "name";
    
                        DataRow firstrow = table.Rows[0];
                        firstrow["name"] = "sample name 1";
                        Console.WriteLine(adapter.Update(table));
                    }
                }
            }
        }
    
    }
    
    Imports Devart.Data.MongoDB
    ...
    Module Module1
    
        Sub Main()
    
            Const connectionString As String = "Host=localhost;Database=sample;User Id=user;Password=pwd;License Key=***"
            Const sql As String = "SELECT _id, name FROM customers"
    
            Using connection As New MongoDBConnection(connectionString)
    
                connection.Open()
                Dim table As New DataTable("customers")
    
                Using command As MongoDBCommand = connection.CreateCommand()
    
                    command.CommandText = sql
                    Using adapter As New MongoDBDataAdapter(command)
                        adapter.Fill(table)
    
                        adapter.UpdateCommand = New MongoDBCommand("UPDATE customers SET name = :name WHERE _id = :id", connection)
    
                        adapter.UpdateCommand.Parameters.Add("id", DbType.String).SourceColumn = "_id"
                        adapter.UpdateCommand.Parameters("id").SourceVersion = DataRowVersion.Original
                        adapter.UpdateCommand.Parameters.Add("name", DbType.String).SourceColumn = "name"
    
                        Dim firstrow As DataRow = table.Rows(0)
                        firstrow("name") = "sample name 1"
                        Console.WriteLine(adapter.Update(table))
                    End Using
                End Using
            End Using
        End Sub
    
    End Module
    

    The following example updates MongoDB data using MongoDBCommand.

    using Devart.Data.MongoDB;
    ...
    class Program
    {
    
        static void Main(string[] args) {
    
            const string connectionString = "Host=localhost;Database=sample;User Id=user;Password=pwd;License Key=***";
            const string sql = "UPDATE customers SET \"address.city\" = 'Berlin' WHERE name = 'sample name 1'";
    
            using (MongoDBConnection connection = new MongoDBConnection(connectionString)) {
    
                connection.Open();
                using (MongoDBCommand command = connection.CreateCommand()) {
    
                    command.CommandText = sql;
                    Console.WriteLine(command.ExecuteNonQuery());
                }
            }
        }
    }
    
    Imports Devart.Data.MongoDB
    ...
    Module Module1
    
        Sub Main()
    
            Const connectionString As String = "Host=localhost;Database=sample;User Id=user;Password=pwd;License Key=***"
            Const sql As String = "UPDATE customers SET ""address.city"" = 'Berlin' WHERE name = 'sample name 1'"
    
            Using connection As New MongoDBConnection(connectionString)
    
                connection.Open()
                Using command As MongoDBCommand = connection.CreateCommand()
    
                    command.CommandText = sql
                    Console.WriteLine(command.ExecuteNonQuery())
                End Using
            End Using
        End Sub
    
    End Module
    

    See Also

    Entity Framework Core  | Retrieving Data