MongoDB C# .NET – Update Nested array examples

Insert element into nested array in Mongodb,MongoDB C# .NET - Update Nested array

Today in this article will see MongoDB C# .NET – Update Nested array examples in .NET or .NET Core application.

We already looked at a simple way of adding or updating a nested array field to the document in general. We shall use positional operator $[ ] for updating the array field .

Today in this article, we will cover below aspects,

Please visit the below article to understand other basic simple scenarios on adding or updating mongo documents,

We shall use the below sample JSON document and will try to update the below-highlighted value for the given filter criteria.

Sample Schema example

{
  "UserId": 999999,
  "DateAdded": "TEST2",
  "Books": [
    {
      "book_name": "book_name1",
      "price": 88,
      "config": [
        {
          "key": "Test1",
          "value": "60"
        }
      ]
    },
    {
      "book_name": "book_name2",
      "price": 50,
      "config": [
        {
          "key": "test2",
          "value": "60"
        },
        {
          "key": "test4",
          "value": "100"
        }
      ]
    }
  ]
}

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 update deeply nested array documents.

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

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

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

Step II – MongoDB Update nested array

 await collection.UpdateOneAsync(x => x.userid == inputUserId,
                Builders<Library>.Update.Set("books.$[g].config.$[c].value", inputValue),
                new UpdateOptions
                {
                    ArrayFilters = new List<ArrayFilterDefinition>
                    {
                        new BsonDocumentArrayFilterDefinition<BsonDocument>(new BsonDocument("g.book_name", inputBookName)),
                        new BsonDocumentArrayFilterDefinition<BsonDocument>(new BsonDocument("c.key", key))
                    }
                });

In the above code, the positional operator $[] identifies the array elements that match the arrayFilters conditions for an update operation.

MongoDB Update nested array

After the successful execution of the query, we shall see the value gets updated correctly.

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 *