MongoDB – C#.NET Add/Update new field to document in a MongoDB Collection -II

mongodb bulkwriteasync

Today, this article will see how to add or update new fields to the MongoDB document programmatically using C# MongoDB driver in .NET or .NET Core application with certain matching criteria.

We already looked at a simple way of adding or updating a new field to the document in general.

Please visit the below article to understand the scenarios,

We will also see how to add a new field to the document in the MongoDB collection using the command line argument.

Getting started

Please create any .NET/.NET Core application.

Please add the MongoDB driver NuGet package using the Nuget Package manager.

PM> Install-Package MongoDB.Driver -Version 2.9.3

I shall keep the MongoDB driver interface used here simple enough to concentrate on how to add new fields to existing documents.

Please visit for better approaches like using DI or IOC here,

Step I – Establish the connection to the Database using the MongoDB driver

var _mongoClient = new MongoClient("mongodb://your connection string");
var db = _mongoClient.GetDatabase("Book");

Step II – Get all the Records from the given collection,

More Typesafe way,

IMongoCollection<Book> collection = db.GetCollection<Book>("Book");

OR

Generic way

var collection = db.GetCollection<BsonDocument>("Book");

Note: Above logic will load the full database. If you need to implement batches or update in chunks asynchronously, please visit the below article for an approach,

Here below is the actual sample schema or document we shall update,

As an example, Let’s now update the fields ‘DateAdded‘ by an additional by 2 Days. Here each date is different for each record. Adding 2 days to existing dates here will update the date field value by two days.

Likewise, you can also update any other fields as required.

Hence we shall first fetch the existing record date and then append it by 2 days using AddDays() method. Likewise, you can use AddSecond or AddMinute, an, etc method suitable for your need.

Generic way using BsonDocument- Update document

MongoDB AddUpdate new field

OR

Typesafe- Update document

Using the typesafe Book model entity,

blank

My observation – Query with type-safe model entity performed better considering the overall performance to update the data.

Once executed above, you can see the date fields got updated by + 2 days.

blank

What is Upsert?

Here Upsert is = Update || Insert

  • That means if the field doesn’t exist in the document, the command will perform Insert and add that new field.
  • If the fields already exist then the command will perform Update will be performed with the new value specified.

That’s all! Happy Coding!

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



4 thoughts on “MongoDB – C#.NET Add/Update new field to document in a MongoDB Collection -II

      1. hi, thanks for the answer. As it turned out, the method really works. It’s me, the fool, that I compiled the filter incorrectly, so I updated the wrong records that I expected. when I figured it out, I went back to your site to correct my comment and thank you for such a good tutorial. Unfortunately, your site does not provide the ability to correct comments, so I could not tell you that everything is fine now.

Leave a Reply

Your email address will not be published. Required fields are marked *