MongoDB GroupBy to set status

MongoDBDatabaseBig Data Analytics

<p>For this, you can use aggregate() in MongoDB. Let us create a collection with documents &minus;</p><pre class="prettyprint notranslate">&gt; db.demo149.insertOne({&quot;Status&quot;:40}); { &nbsp; &nbsp;&quot;acknowledged&quot; : true, &nbsp; &nbsp;&quot;insertedId&quot; : ObjectId(&quot;5e350386fdf09dd6d08539c4&quot;) } &gt; db.demo149.insertOne({&quot;Status&quot;:40}); { &nbsp; &nbsp;&quot;acknowledged&quot; : true, &nbsp; &nbsp;&quot;insertedId&quot; : ObjectId(&quot;5e350388fdf09dd6d08539c5&quot;) } &gt; db.demo149.insertOne({&quot;Status&quot;:50}); { &nbsp; &nbsp;&quot;acknowledged&quot; : true, &nbsp; &nbsp;&quot;insertedId&quot; : ObjectId(&quot;5e35038afdf09dd6d08539c6&quot;) }</pre><p>Display all documents from a collection with the help of find() method &minus;</p><pre class="prettyprint notranslate">&gt; db.demo149.find();</pre><p>This will produce the following output &minus;</p><pre class="result notranslate">{ &quot;_id&quot; : ObjectId(&quot;5e350386fdf09dd6d08539c4&quot;), &quot;Status&quot; : 40 } { &quot;_id&quot; : ObjectId(&quot;5e350388fdf09dd6d08539c5&quot;), &quot;Status&quot; : 40 } { &quot;_id&quot; : ObjectId(&quot;5e35038afdf09dd6d08539c6&quot;), &quot;Status&quot; : 50 }</pre><p>Here is the query to MongoDB group by &minus;</p><pre class="prettyprint notranslate">&gt; db.demo149.aggregate([ ... &nbsp; &nbsp;{ ... &nbsp; &nbsp; &nbsp; &quot;$group&quot;: { ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;_id&quot;: null, ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;done&quot;: { ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;$push&quot;: { ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;$cond&quot;: [ ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &quot;$eq&quot;: [ &quot;$Status&quot;, 40 ] }, ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &quot;_id&quot;: &quot;$_id&quot;, &quot;Status&quot;: &quot;$Status&quot; }, ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; false ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;] ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}, ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;notdone&quot;: { ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;$push&quot;: { ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;$cond&quot;: [ ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &quot;$eq&quot;: [ &quot;$Status&quot;, 50 ] }, ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &quot;_id&quot;: &quot;$_id&quot;, &quot;Status&quot;: &quot;$Status&quot; }, ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; false ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;] ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} ... &nbsp; &nbsp; &nbsp; } ... &nbsp; &nbsp;}, ... { ... &nbsp; &nbsp;&quot;$project&quot;: { ... &nbsp; &nbsp; &nbsp; &quot;_id&quot;: 0, ... &nbsp; &nbsp; &nbsp; &quot;done&quot;: { ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;$setDifference&quot;: [ &quot;$done&quot;, [false] ] ... &nbsp; &nbsp; &nbsp; }, ... &nbsp; &nbsp; &nbsp; &quot;notdone&quot;: { ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;$setDifference&quot;: [ &quot;$notdone&quot;, [false] ] ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} ... &nbsp; &nbsp; &nbsp; } ... &nbsp; &nbsp;} ... ]);</pre><p>This will produce the following output &minus;</p><pre class="result notranslate">{ &nbsp; &nbsp;&quot;done&quot; : [ &nbsp; &nbsp; &nbsp; { &quot;_id&quot; : ObjectId(&quot;5e350386fdf09dd6d08539c4&quot;), &quot;Status&quot; : 40 }, &nbsp; &nbsp; &nbsp; { &quot;_id&quot; : ObjectId(&quot;5e350388fdf09dd6d08539c5&quot;), &quot;Status&quot; : 40 } ], &quot;notdone&quot; : [ &nbsp; &nbsp; &nbsp; { &quot;_id&quot; : ObjectId(&quot;5e35038afdf09dd6d08539c6&quot;), &quot;Status&quot; : 50 } &nbsp; &nbsp;] }</pre>
raja
Updated on 31-Mar-2020 12:55:01

Advertisements