<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>update document requires atomic operators - TheCodeBuzz</title>
	<atom:link href="https://thecodebuzz.com/tag/update-document-requires-atomic-operators/feed/" rel="self" type="application/rss+xml" />
	<link>https://thecodebuzz.com</link>
	<description>Best Practices for Software Development</description>
	<lastBuildDate>Sat, 08 Apr 2023 00:42:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://thecodebuzz.com/wp-content/uploads/2022/11/cropped-android-chrome-512x512-1-1-51x51.jpg</url>
	<title>update document requires atomic operators - TheCodeBuzz</title>
	<link>https://thecodebuzz.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>MongoDB- How to Update And Return Changed Document In C#.NET</title>
		<link>https://thecodebuzz.com/mongodb-update-return-document-after-and-before-change/</link>
					<comments>https://thecodebuzz.com/mongodb-update-return-document-after-and-before-change/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 27 Feb 2021 02:56:00 +0000</pubDate>
				<category><![CDATA[Guidelines]]></category>
		<category><![CDATA[mongodb update and return updated document]]></category>
		<category><![CDATA[mongodb update document]]></category>
		<category><![CDATA[mongodb update document by _id]]></category>
		<category><![CDATA[mongodb update example]]></category>
		<category><![CDATA[mongodb update multiple documents]]></category>
		<category><![CDATA[mongodb updatemany]]></category>
		<category><![CDATA[mongodb updateone return updated document]]></category>
		<category><![CDATA[update document requires atomic operators]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=16172</guid>

					<description><![CDATA[<p>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 [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/mongodb-update-return-document-after-and-before-change/">MongoDB- How to Update And Return Changed Document In C#.NET</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">MongoDB- How to Update And Return Changed Document</h1>



<figure class="wp-block-image size-large is-resized"><img fetchpriority="high" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2021/05/mongodb-return-both-before-and-after-document-1024x292.jpg" alt="Mongo ReturnDocument,MongoDB- Update And Return Changed Document,Return Changed Document In C#.NET 
" class="wp-image-16177" width="811" height="231" srcset="https://thecodebuzz.com/wp-content/uploads/2021/05/mongodb-return-both-before-and-after-document-1024x292.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/05/mongodb-return-both-before-and-after-document-300x86.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/05/mongodb-return-both-before-and-after-document-768x219.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/05/mongodb-return-both-before-and-after-document-1536x438.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/05/mongodb-return-both-before-and-after-document-785x224.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/05/mongodb-return-both-before-and-after-document.jpg 1633w" sizes="(max-width: 811px) 100vw, 811px" /></figure>



<p>Today this article will see how to perform a <em><strong>MongoDB- How to Update And </strong></em>Return Changed Document In C#.NET <em><strong>after the modification</strong></em>. </p>



<p></p>



<p>Update And return changed documents is easy using below techniques discussed.</p>



<p></p>



<p>We will also how to return the Mongo document<em><strong> before the modification.</strong></em></p>



<p></p>



<p>This could be a useful feature for many other use cases including versioning the data based on any updates to the Mongo document.</p>



<p></p>



<p>Today in this article, we will cover below aspects,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-establish-the-connection-to-the-database-using-mongodb-c-driver">Establish the connection to the Database using MongoDB C# driver</a></li><li><a href="#aioseo-returndocument-after-mongodb-update-and-return-document-after-the-change">ReturnDocument.After &#8211; MongoDB Update And Return Document after the change</a></li><li><a href="#aioseo-returndocument-before-mongodb-update-and-return-document-before-change">ReturnDocument.Before- MongoDB Update And Return Document before Change</a></li></ul></div>



<p></p>



<p>We shall see <strong>C# MongoDB</strong> driver in .NET or .NET Core example to perform the same.</p>



<p></p>



<p>The below query used and explained is just for demonstration purposes, however, the concept can be used for any of your custom query requirements.</p>



<p></p>



<p>Let&#8217;s use the below sample&nbsp;<strong><em>JSON&nbsp;</em></strong>document. We will update the document for one more array field in the array &#8216;<strong><em>SubCategories</em></strong>&#8216;. </p>



<p></p>



<p>Once updated successfully, we will return the document that got updated.</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
{
  &quot;userId&quot;: &quot;12345&quot;,
  &quot;Name&quot;: &quot;Categories&quot;,
  &quot;Books&quot;: &#x5B;
    {
      &quot;name&quot;: &quot;C# Book&quot;,
      &quot;catId&quot;: &quot;5555&quot;,
    },
   {
      &quot;name&quot;: &quot;Typescript Book&quot;,
      &quot;catId&quot;: &quot;44444&quot;,
    }
  ],
 &quot;createdDate&quot;:&quot;2020-09-20T21:07:42.313+00:00&quot;
}
</pre></div>


<p></p>



<h2 class="wp-block-heading" id="aioseo-establish-the-connection-to-the-database-using-mongodb-c-driver">Establish the connection to the Database using MongoDB C# driver </h2>



<p></p>



<p>Below is a basic skeleton code to establish the connection to the Database using the MongoDB C# driver.</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
var _mongoClient = new MongoClient(&quot;mongodb://your connection string&quot;);

var db = _mongoClient.GetDatabase(&quot;Library&quot;);

ar collection = db.GetCollection(&quot;Books&quot;);
</pre></div>


<p></p>



<h2 class="wp-block-heading" id="aioseo-returndocument-after-mongodb-update-and-return-document-after-the-change">ReturnDocument.After &#8211; MongoDB Update And Return Document after the change</h2>



<p></p>



<p>Let&#8217;s update the records using <strong><em>FindOneAndUpdateAsync </em></strong>and return the updated document,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
var filter = Builders&lt;BsonDocument&gt;.Filter.And(
             Builders&lt;BsonDocument&gt;.Filter.Eq(&quot;userId&quot;, userId)); 

            var update = Builders&lt;BsonDocument&gt;.Update.Set(&quot;createdDate&quot;, DateTime.UtcNow);

            var result = await collection.FindOneAndUpdateAsync(filter, update,
                options: new FindOneAndUpdateOptions&lt;BsonDocument, BsonDocument&gt;
                {
                    IsUpsert = true,
                    ReturnDocument = ReturnDocument.After
                });


</pre></div>


<p></p>



<p><em>Query explained &#8211; </em></p>



<ul class="wp-block-list">
<li><strong><em>createdDate </em></strong>field will be updated with today&#8217;s current date after the successful updates.</li>
</ul>



<ul class="wp-block-list">
<li><strong><em>FindOneAndUpdateOptions </em></strong>with options as &#8220;<strong><em>ReturnDocument.After</em></strong>&#8221; gives a version of the document after the updates are performed.</li>
</ul>



<p></p>



<p>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.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-returndocument-before-mongodb-update-and-return-document-before-change">ReturnDocument.Before- MongoDB Update And Return Document before Change</h2>



<p></p>



<p>You can use <strong><em>FindOneAndUpdateOptions </em></strong>with options as &#8220;<strong><em>ReturnDocument.Before</em></strong>&#8220;. </p>



<p></p>



<p>This option will give you a version of the document before the updates happen.</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
var result = await collection.FindOneAndUpdateAsync(filter, update,
                options: new FindOneAndUpdateOptions&lt;BsonDocument, BsonDocument&gt;
                {
                    IsUpsert = true,
                    ReturnDocument = ReturnDocument.Before
                });
</pre></div>


<p></p>



<p><strong><em>Reference </em></strong></p>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/insert-push-element-into-nested-array-mongodb/" title="Insert element into nested array in MongoDB" target="_blank" rel="noreferrer noopener"><em>Insert an element into nested array in MongoDB</em></a></li>
</ul>



<ul class="wp-block-list">
<li><em><a href="https://www.thecodebuzz.com/mongo-db-naming-conventions-standards-guidelines/" target="_blank" rel="noreferrer noopener" title="MongoDB naming Conventions ">MongoDB naming Conventions </a></em></li>
</ul>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/mongodb-update-return-document-after-and-before-change/">MongoDB- How to Update And Return Changed Document In C#.NET</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/mongodb-update-return-document-after-and-before-change/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
