{"id":10948,"date":"2013-10-04T20:35:56","date_gmt":"2013-10-04T15:05:56","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=10948"},"modified":"2016-12-19T15:31:03","modified_gmt":"2016-12-19T10:01:03","slug":"group-by-in-mongodbwith-group","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/group-by-in-mongodbwith-group\/","title":{"rendered":"Group By in MongoDB(with $group)"},"content":{"rendered":"<p><strong>MongoDB<\/strong> provides a rich set of <strong>aggregation<\/strong>. <strong>$group<\/strong> is one of them, with the help of <strong>$group<\/strong>, we can <strong>group<\/strong>  documents (similar as <strong>group By<\/strong> in <strong>SQL<\/strong>). Lets understand by an example:<\/p>\n<p>Suppose we have following collection of blogs:<br \/>\n[js]<br \/>\n{ userId:1, name:&#8221;Suroor&#8221;, title:&#8221;AngularJS: Getting started with Directive&#8221;, rating:3}<br \/>\n{ userId:2, name:&#8221;Amit&#8221;, title:&#8221;AngularJS: Getting started with Directive&#8221;, rating:5}<br \/>\n{ userId:3, name:&#8221;PI&#8221;, title:&#8221;Prototype in Javascript&#8221;, rating:4}<br \/>\n{ userId:2, name:&#8221;Amit&#8221;, title:&#8221;this keyword in Javascript&#8221;, rating:5}<br \/>\n[\/js]<br \/>\nAnd now we want to <strong>group<\/strong> all the blogs by userId and rating greater then 3:<br \/>\n[js]<br \/>\ndb.blogs.aggregate([{$match:{rating:{$gt:3}}}, {$group:{ _id: &#8216;$userId&#8217;, title: { $push: &#8216;$title&#8217;}}}]);<br \/>\n[\/js]<br \/>\nThis will return <strong>grouped<\/strong> result of all the blogs with id and title whose rating is greater then <strong>3<\/strong>. Here <strong>$match<\/strong> will match the condition, <strong>$group<\/strong> groups all the documents by field-name which is provided as _id and <strong>$push<\/strong> will add the title into result.<\/p>\n<p><strong>OUTPUT:<\/strong><br \/>\n[js]<br \/>\n{<br \/>\n\t&#8220;result&#8221; : [<br \/>\n\t\t{<br \/>\n\t\t\t&#8220;_id&#8221; : 3,<br \/>\n\t\t\t&#8220;title&#8221; : [<br \/>\n\t\t\t\t&#8220;Prototype in Javascript&#8221;<br \/>\n\t\t\t]<br \/>\n\t\t},<br \/>\n\t\t{<br \/>\n\t\t\t&#8220;_id&#8221; : 2,<br \/>\n\t\t\t&#8220;title&#8221; : [<br \/>\n\t\t\t\t&#8220;AngularJS: Getting started with Directive&#8221;,<br \/>\n\t\t\t\t&#8220;this keyword in Javascript&#8221;<br \/>\n\t\t\t]<br \/>\n\t\t}<br \/>\n\t],<br \/>\n\t&#8220;ok&#8221; : 1<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>Amit Kumar<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit.kumar\/\">amit.kumar@intelligrape.com<\/a><br \/>\nin.linkedin.com\/in\/amitkumar0110<br \/>\ntwitter.com\/amit_kumar0110<br \/>\n<strong><a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit-kumar\/\">More Blogs by Me<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB provides a rich set of aggregation. $group is one of them, with the help of $group, we can group documents (similar as group By in SQL). Lets understand by an example: Suppose we have following collection of blogs: [js] { userId:1, name:&#8221;Suroor&#8221;, title:&#8221;AngularJS: Getting started with Directive&#8221;, rating:3} { userId:2, name:&#8221;Amit&#8221;, title:&#8221;AngularJS: Getting started [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1],"tags":[1223,1149,1151,4846],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/10948"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=10948"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/10948\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=10948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=10948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=10948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}