MongoDB – Add/Update field to document using BulkWriteAsync

BulkWriteAsync

Today this article will see how to add or update new fields to the MongoDB document programmatically using the C# MongoDB driver in a .NET or .NET Core application. We shall see the use of the BulkWriteAsync an asynchronous extension method from MongoDB Driver.

Today in this article, we will cover below aspects,

We already looked at a simple way of adding or updating a new field to the document in our previous MongoDB sample series.

Please visit the below article to understand the scenarios,

We will also see how to add a new field to the document in 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 <>

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 Database using MongoDB driver ,

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

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

More Typesafe way,

Example

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

Or

Example

Generic way

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

Note: If you need to implement batch update or chunk update asynchronously, please visit the below article.

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

Let’s now update the fields ‘DateAdded‘ by an additional by 2 Days. Here each date is different for each record. Likewise, you can also update any other fields as required.

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

In the below example, we have defined a filter with search criteria where we have used matching Id. Then for each match, we update the required data for the date.

Example using BulkWriteAsync

OR

Example using typesafe BulkWriteAsync

Using the typesafe Book model entity,

blank

Note– Above I have defined bulkUpdatModel as a list of all models that require an update or insert.

I also have upsertOne which allows you to specify Upsert’s true or false flag which is very useful when dealing with new inserts and regular updates to existing fields.

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

Once executed above, you can see 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 to the document with a given value.
  • If the fields already exist then the command will perform Update the same field with the a 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.



Leave a Reply

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