{"id":10162,"date":"2013-04-02T16:42:26","date_gmt":"2013-04-02T11:12:26","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=10162"},"modified":"2013-05-15T15:11:55","modified_gmt":"2013-05-15T09:41:55","slug":"average-in-mongodb","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/average-in-mongodb\/","title":{"rendered":"How to Determine Average in MongoDB"},"content":{"rendered":"<p>MongoDB provides several ways of computing the average value of a group in a collection. One of the simplest ways of determining average is using the method<strong> db.collection.group()<\/strong>.<br \/>\n<br \/>\nThe method db.collection.group() bunches the documents of a collection on the basis of the keys mentioned and executes aggregation functions on them. This method can be used for performing several operations, including count and addition. In order words, the method performs functionality similar to that of the SQL\u2019s Select-Group By combination.<br \/>\n<br \/>\nThe implementation and working of the method db.collection.group() for determining the average of a field in a collection is illustrated here using the example of a collection \u2018userquestions\u2019. The query, which can be used to find average, is as follows &#8211;<\/p>\n<p>[shell]<\/p>\n<p>var reduceFunction = function(currentDocument, previousDocument) {<br \/>\n     previousDocument.totalRating += currentDocument.rating; previousDocument.count += 1<br \/>\n};<\/p>\n<p>var finalizeFunction = function(currentDocument) {<br \/>\n     currentDocument.average = currentDocument.totalRating\/currentDocument.count;<br \/>\n     delete currentDocument.totalRating;<br \/>\n     delete currentDocument.count;<br \/>\n};<\/p>\n<p>db.userquestions.group({<br \/>\n     key: {&#8216;quesId&#8217;: true},<br \/>\n     initial: {totalRating: 0, count:0},<br \/>\n     reduce: reduceFunction,<br \/>\n     finalize:finalizeFunction<br \/>\n});<\/p>\n<p>[\/shell]<\/p>\n<p>In the present context, the value of the \u2018key\u2019 is the field \u2018quesId\u2019. Documents are grouped together on the basis of this key. The initial values for the parameters \u2018count\u2019 and \u2018totalRating\u2019 are set to zero. Besides these, two functions are declared and defined in the query. The function <strong>reduce<\/strong> totals the value of the \u2018rating\u2019 and saves it in \u2018totalRating\u2019. In addition to this, it also maintains the count of documents processed.<br \/>\n<br \/>\nOn the other hand, the function \u2018finalize\u2019 determines the average value of rating by dividing the value of \u2018totalRating\u2019 by the count.The execution of this query returns an array containing different \u2018quesId\u2019, which represents the different groups and the corresponding average rating for each of these groups.<br \/>\n<br \/>\nHope this will help. \ud83d\ude42<\/p>\n<p>Thanks,<br \/>\nSakshi Tyagi<br \/>\nsakshi@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB provides several ways of computing the average value of a group in a collection. One of the simplest ways of determining average is using the method db.collection.group(). The method db.collection.group() bunches the documents of a collection on the basis of the keys mentioned and executes aggregation functions on them. This method can be used [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1],"tags":[1150,1149,1151,55,4846],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/10162"}],"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\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=10162"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/10162\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=10162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=10162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=10162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}