Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is that you want to accumulate all the errors for each key and that means you have to emit an array of documents and then your reduce has to combine them into a single array (as it's going to receive an array of arrays).</p> <p>The other problem you have is that you want to combine different classes of each unique cid into some combined reduced result, but your reduce function is not even trying to do that.</p> <p>It's hard to know how many different class values you might get for each sid but if it's like the example data you gave, I'm guessing you want something like this:</p> <pre><code>var m = function() { array = []; if (this.class == "E") { value = { time: this.time, error: this.text }; array.push(value); emit(this.sid, {a:array}); } else if (this.class == "F") { value = { service: this.service }; array.push(value); emit(this.sid, {a:array}); } } var r = function(key,values) { result = {a:[]}; values.forEach(function (v) { result.a = v.a.concat(result.a); }); return result; } db.events.mapReduce(m,r,"errors"); </code></pre> <p>On your example, the resultant collection will look like this:</p> <pre><code>&gt; db.errors.find().pretty() { "_id" : "1UADM45MCGRTW ", "value" : { "a" : [ { "service" : "service1" }, { "time" : ISODate("2013-04-08T11:33:13.236Z"), "error" : "error message" } ] } } { "_id" : "1UDO3H7YUOK08 ", "value" : { "a" : [ { "service" : "service2" }, { "time" : ISODate("2013-04-08T11:33:08.173Z"), "error" : "another error message" } ] } } { "_id" : "1UDO3JVXIS2UZ ", "value" : { "a" : [ { "service" : "service1" } ] } } </code></pre> <p>You can probably add a finalize function to transform this array into a result that's a single document but that depends on the format of the resultant collection that you want to have and on the expected input for each sid.</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