MongoDB C#.NET – How to remove or delete field query

MongoDb remove field

Today this article will see MongoDB C#.NET – How to remove field query using .NET C# programmatically. We will be using the MongoDB C# .NET driver.

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

Today in this article, we will cover below aspects,

Please visit the below article for criteria-based Updates/Add to an existing document in MongoDB.

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

Note – Please use the latest version available.

Again, I shall keep the Mongo driver interface use simple here to concentrate on how to rename fields from existing documents.

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

Step I – Establish the connection with 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.

var collection = db.GetCollection("employee");

I have below Mongo Collection where mongo schema and their fields are defined as below.

remove fieldMongoDB CNET How to remove field query

I have a field name called “EmpID” which I want to delete programmatically using C# code.

C# code has the below command equivalent method called Unset() which we will use below to remove the field.

Command

{ $unset: { <fieldName>:1} }

You can specify one or multiple fields for the renaming support.

Asynchronous removal or delete of fields using Unset

C# query in a Generic way using BsonDocument

foreach (var rootObject in documents)
            {
                var update = Builders<BsonDocument>.Update.Unset("EmpID");
                var filter = Builders<BsonDocument>.Filter.Eq("_id", (ObjectId)rootObject.GetElement("_id").Value);
                await collection.UpdateOneAsync(filter, update);

            }

  • We are using the UpdateOneAsync command which lets you perform updates for each record using “_id” as filter criteria.
  • Above Update.Unset method let you specify the field name to be deleted from the Mongo document.

Synchronous Remove operation

You can use UpdateOne.

collection.UpdateOne(filter, update, options);

Remove field query only if fields exist in MongoDB

You can write a filter query beforehand for the given fields like verifying if that fields exist and then deleting them.

MongoDB CNET How to remove field query

One can also try using UpdateMany for bulk updates of fields if dealing with smaller databases.

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

Please sound off your comments below.

Happy Coding !!

References:



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 *