Combine update and query parts to form the upserted document in MongoDB?

MongoDBDatabaseBig Data Analytics

<p>You need to use $set operator along with upsert:true. Let us first create a collection with documents &minus;</p><pre class="prettyprint notranslate">&gt; db.updateWithUpsertDemo.insertOne({&quot;StudentFirstName&quot;:&quot;John&quot;,&quot;StudentAge&quot;:21}); { &nbsp; &nbsp;&quot;acknowledged&quot; : true, &nbsp; &nbsp;&quot;insertedId&quot; : ObjectId(&quot;5cd2a61c345990cee87fd890&quot;) } &gt; db.updateWithUpsertDemo.insertOne({&quot;StudentFirstName&quot;:&quot;Larry&quot;,&quot;StudentAge&quot;:23}); { &nbsp; &nbsp;&quot;acknowledged&quot; : true, &nbsp; &nbsp;&quot;insertedId&quot; : ObjectId(&quot;5cd2a624345990cee87fd891&quot;) } &gt; db.updateWithUpsertDemo.insertOne({&quot;StudentFirstName&quot;:&quot;David&quot;,&quot;StudentAge&quot;:24}); { &nbsp; &nbsp;&quot;acknowledged&quot; : true, &nbsp; &nbsp;&quot;insertedId&quot; : ObjectId(&quot;5cd2a62c345990cee87fd892&quot;) }</pre><p>Following is the query to display all documents from a collection with the help of find() method &minus;</p><pre class="prettyprint notranslate">&gt; db.updateWithUpsertDemo.find().pretty();</pre><p>This will produce the following output &minus;</p><pre class="result notranslate">{ &nbsp; &nbsp;&quot;_id&quot; : ObjectId(&quot;5cd2a61c345990cee87fd890&quot;), &nbsp; &nbsp;&quot;StudentFirstName&quot; : &quot;John&quot;, &nbsp; &nbsp;&quot;StudentAge&quot; : 21 } { &nbsp; &nbsp;&quot;_id&quot; : ObjectId(&quot;5cd2a624345990cee87fd891&quot;), &nbsp; &nbsp;&quot;StudentFirstName&quot; : &quot;Larry&quot;, &nbsp; &nbsp;&quot;StudentAge&quot; : 23 } { &nbsp; &nbsp;&quot;_id&quot; : ObjectId(&quot;5cd2a62c345990cee87fd892&quot;), &nbsp; &nbsp;&quot;StudentFirstName&quot; : &quot;David&quot;, &nbsp; &nbsp;&quot;StudentAge&quot; : 24 }</pre><p>Following is the query to combine update and query parts to form the upserted document &minus;</p><pre class="prettyprint notranslate">&gt; db.updateWithUpsertDemo.update({_id: ObjectId(&quot;5cd2a624345990cee87fd891&quot;)},{&quot;$set&quot;: {&quot;StudentFirstName&quot;: &quot;Chris&quot;}}, {upsert:true}); WriteResult({ &quot;nMatched&quot; : 1, &quot;nUpserted&quot; : 0, &quot;nModified&quot; : 1 })</pre><p>Let us check whether the field &ldquo;StudentFirstName&rdquo; has been changed or not &minus;</p><pre class="prettyprint notranslate">&gt; db.updateWithUpsertDemo.find().pretty();</pre><p>This will produce the following output &minus;</p><pre class="result notranslate">{ &nbsp; &nbsp;&quot;_id&quot; : ObjectId(&quot;5cd2a61c345990cee87fd890&quot;), &nbsp; &nbsp;&quot;StudentFirstName&quot; : &quot;John&quot;, &nbsp; &nbsp;&quot;StudentAge&quot; : 21 } { &nbsp; &nbsp;&quot;_id&quot; : ObjectId(&quot;5cd2a624345990cee87fd891&quot;), &nbsp; &nbsp;&quot;StudentFirstName&quot; : &quot;Chris&quot;, &nbsp; &nbsp;&quot;StudentAge&quot; : 23 } { &nbsp; &nbsp;&quot;_id&quot; : ObjectId(&quot;5cd2a62c345990cee87fd892&quot;), &nbsp; &nbsp;&quot;StudentFirstName&quot; : &quot;David&quot;, &nbsp; &nbsp;&quot;StudentAge&quot; : 24 }</pre>
raja
Updated on 30-Jul-2019 22:30:26

Advertisements