Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoDb Aggregate function issue with php
    text
    copied!<p>I am new to mongoDb and facing issue in using aggregate function. I am trying to get sum of the fields "expectations" and "overall" but it returns 0. I also want to take the total count of the comments which are not empty or null in the same query. </p> <pre><code>$out = $collection-&gt;aggregate ( array( array( '$match' =&gt; array( 'id' =&gt; 6200 )), array ('$unwind' =&gt; '$reviews'), array( '$group' =&gt; array( '_id' =&gt; '$id', 'exptotal' =&gt; array( '$sum' =&gt; array('reviews' =&gt; '$expectations') ), 'total' =&gt; array( '$sum' =&gt; array('reviews' =&gt; '$overall' ) ), 'count' =&gt; array( '$sum' =&gt; 1 ) ) ) ) ); </code></pre> <p>Here is the json</p> <pre><code>{ "_id": "528c62406a542f7c6a6bf522", "id": 6200, "categories": [ { "id": 6, "name": "Artificial Intelligence" }, { "id": 5, "name": "Statistics and Data Analysis" } ], "courseId": "COURSE_16", "institute": { "id": 5693, "name": "YZ University" }, "instructors": [ " A Morris" ], "language": "en", "reviews": [ { "username": "kalis", "expectations": 3, "content": 2, "overall": 3, "comments": "This is really good course for improvement", "datecreated": "2013-11-02T17:04:11.102Z" }, { "username": "julia", "expectations": 4, "content": 2, "overall": 2, "comments": "This improves my skill a lot", "datecreated": "2013-11-03T17:04:11.102Z" }, { "username": "john", "expectations": 2, "content": 4, "overall": 4, "comments": "", "datecreated": "2013-11-04T17:04:11.102Z" } ], "shortName": "ml", "title": "Machine Learning" </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload