Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are several issues with your sample document and aggregation:</p> <ul> <li>the sample doc will not match your aggregation query because you are matching on <code>createdDate</code> field existing</li> <li>the <a href="http://docs.mongodb.org/manual/reference/aggregation/group/"><code>$group()</code> operator</a> works on a document level and needs an <code>_id</code> field to group by</li> <li>your <code>list</code> field is an embedded document, not an array</li> <li>aside from formatting, there is no obvious way to relate the sample values to the calculated result you are looking for</li> </ul> <p>Here is an adjusted example document with the <code>list</code> as an array as well as some unique values for each item that happen to add up to the <code>value</code> numbers you mentioned:</p> <pre><code>db.people.insert({ "ad" : "noc2", "createdDate" : ISODate(), "list" : [ { "id" : "p45", "date" : ISODate("2014-01-01T12:18:30.568Z"), "value3" : 21, "value1" : 77, "value2" : 489 }, { "id" : "p6", "date" : ISODate("2013-07-18T12:18:30.568Z"), "value3" : 20, "value1" : 20, "value2" : 7 }, { "id" : "4578", "date" : ISODate("2013-07-18T12:18:30.568Z"), "value3" : 21, "value1" : 300, "value2" : -319 } ] }) </code></pre> <p>You can now use the <a href="http://docs.mongodb.org/manual/reference/aggregation/unwind/"><code>$unwind</code> operator</a> to extract the matching subdocuments.</p> <p>It is unclear from the current question description what <code>$group</code> operation you are trying to achieve and whether you actually need <code>$group</code>.</p> <p>Here is an example using the Aggregation Framework to <a href="http://docs.mongodb.org/manual/reference/aggregation/add/"><code>$add</code></a> (total) the three values for each list item:</p> <pre><code>db.people.aggregate( // Find relevant docs (can use index if available) { $match: { createdDate: { $exists:true }, "ad":"noc2" }}, // Convert list array to stream of documents { $unwind: "$list" }, // Add (value1, value2, value3) for each list item { $project: { _id: "$list.id", value: { $add: [ "$list.value1", "$list.value2", "$list.value3"] } }} ); </code></pre> <p>Sample output:</p> <pre><code>{ "result" : [ { "_id" : "p45", "value" : 587 }, { "_id" : "p6", "value" : 47 }, { "_id" : "4578", "value" : 2 } ], "ok" : 1 } </code></pre> <p>Note that I've only aggregated using a single document for this example, and the output will be one result document for every item in the <code>list</code> array.</p> <p>In real usage you may want to add an additional aggregation step to <code>$group</code> by document <code>_id</code> or some other criteria, depending on the outcome you are trying to achieve.</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