Note that there are some explanatory texts on larger screens.

plurals
  1. POMongodb Aggregation Framework | Group over multiple values?
    primarykey
    data
    text
    <p>I would like to use mongoDB's Aggregation Framework to run what in SQL would look a bit like:</p> <pre><code>SELECT SUM(A), B, C from myTable GROUP BY B, C; </code></pre> <p>The docs state:</p> <blockquote> <p>You can specify a single field from the documents in the pipeline, a previously computed value, or an aggregate key made up from several incoming fields.</p> </blockquote> <p>But it's unclear what 'an aggregate key made from several incoming fields' actually is?</p> <p>My dataset is a bit like this:</p> <pre><code>[{ "timeStamp" : 1341834988666, "label" : "sharon", "responseCode" : "200", "value" : 10, "success" : "true"}, { "timeStamp" : 1341834988676, "label" : "paul", "responseCode" : "200", "value" : 60, "success" : "true"}, { "timeStamp" : 1341834988686, "label" : "paul", "responseCode" : "404", "value" : 15, "success" : "true"}, { "timeStamp" : 1341834988696, "label" : "sharon", "responseCode" : "200", "value" : 35, "success" : "false"}, { "timeStamp" : 1341834988166, "label" : "paul", "responseCode" : "200", "value" : 40, "success" : "true"}, { "timeStamp" : 1341834988266, "label" : "paul", "responseCode" : "404", "value" : 99, "success" : "false"}] </code></pre> <p>My query looks like this:</p> <pre><code>resultsCollection.aggregate( { $match : { testid : testid} }, { $skip : alreadyRead }, { $project : { timeStamp : 1 , label : 1, responseCode : 1 , value : 1, success : 1 }}, { $group : { _id : "$label", max_timeStamp : { $timeStamp : 1 }, count_responseCode : { $sum : 1 }, avg_value : { $sum : "$value" }, count_success : { $sum : 1 } }}, { $group : { ? }} ); </code></pre> <p>My instinct was to try to pipe the results through to a second group, I know you can do this but it won't work because the first group already reduces the dataset too much and the required level of detail is lost.</p> <p>What I want to do is group using <code>label</code>, <code>responseCode</code> and <code>success</code> and get the sum of value from the result. It should look a bit like:</p> <pre><code>label | code | success | sum_of_values | count sharon | 200 | true | 10 | 1 sharon | 200 | false | 35 | 1 paul | 200 | true | 100 | 2 paul | 404 | true | 15 | 1 paul | 404 | false | 99 | 1 </code></pre> <p>Where there are five groups:</p> <pre><code>1. { "timeStamp" : 1341834988666, "label" : "sharon", "responseCode" : "200", "value" : 10, "success" : "true"} 2. { "timeStamp" : 1341834988696, "label" : "sharon", "responseCode" : "200", "value" : 35, "success" : "false"} 3. { "timeStamp" : 1341834988676, "label" : "paul", "responseCode" : "200", "value" : 60, "success" : "true"} { "timeStamp" : 1341834988166, "label" : "paul", "responseCode" : "200", "value" : 40, "success" : "true"} 4. { "timeStamp" : 1341834988686, "label" : "paul", "responseCode" : "404", "value" : 15, "success" : "true"} 5. { "timeStamp" : 1341834988266, "label" : "paul", "responseCode" : "404", "value" : 99, "success" : "false"} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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