MongoDB- How to Update And Return Changed Document
Today this article will see how to perform a MongoDB- How to Update And Return Changed Document In C#.NET after the modification.
Update And return changed documents is easy using below techniques discussed.
We will also how to return the Mongo document before the modification.
This could be a useful feature for many other use cases including versioning the data based on any updates to the Mongo document.
Today in this article, we will cover below aspects,
We shall see C# MongoDB driver in .NET or .NET Core example to perform the same.
The below query used and explained is just for demonstration purposes, however, the concept can be used for any of your custom query requirements.
Let’s use the below sample JSON document. We will update the document for one more array field in the array ‘SubCategories‘.
Once updated successfully, we will return the document that got updated.
{
"userId": "12345",
"Name": "Categories",
"Books": [
{
"name": "C# Book",
"catId": "5555",
},
{
"name": "Typescript Book",
"catId": "44444",
}
],
"createdDate":"2020-09-20T21:07:42.313+00:00"
}
Establish the connection to the Database using MongoDB C# driver
Below is a basic skeleton code to establish the connection to the Database using the MongoDB C# driver.
var _mongoClient = new MongoClient("mongodb://your connection string");
var db = _mongoClient.GetDatabase("Library");
ar collection = db.GetCollection("Books");
ReturnDocument.After – MongoDB Update And Return Document after the change
Let’s update the records using FindOneAndUpdateAsync and return the updated document,
var filter = Builders<BsonDocument>.Filter.And(
Builders<BsonDocument>.Filter.Eq("userId", userId));
var update = Builders<BsonDocument>.Update.Set("createdDate", DateTime.UtcNow);
var result = await collection.FindOneAndUpdateAsync(filter, update,
options: new FindOneAndUpdateOptions<BsonDocument, BsonDocument>
{
IsUpsert = true,
ReturnDocument = ReturnDocument.After
});
Query explained –
- createdDate field will be updated with today’s current date after the successful updates.
- FindOneAndUpdateOptions with options as “ReturnDocument.After” gives a version of the document after the updates are performed.
After successful execution of the query, we shall see the document is updated with a new date, and the query returns the MongoDB documents with a newly added array element.
ReturnDocument.Before- MongoDB Update And Return Document before Change
You can use FindOneAndUpdateOptions with options as “ReturnDocument.Before“.
This option will give you a version of the document before the updates happen.
var result = await collection.FindOneAndUpdateAsync(filter, update,
options: new FindOneAndUpdateOptions<BsonDocument, BsonDocument>
{
IsUpsert = true,
ReturnDocument = ReturnDocument.Before
});
Reference
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.