Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>MongoDB <a href="http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Group">group by</a> is very limited in most cases, for instance</p> <pre><code>- the result set must be lesser than 10000 keys. - it will not work in sharded environments </code></pre> <p>So its better to use map reduce. so the query would be like this</p> <p><strike> map = function() { emit({a:true,b:true},{count:1}); }</strike> </p> <pre><code>reduce = function(k, values) { var result = {count: 0}; values.forEach(function(value) { result.count += value.count; }); return result; } </code></pre> <p>and then</p> <pre><code>db.list.mapReduce(map,reduce,{out: { inline : 1}}) </code></pre> <p>Its a untested version. let me know if it works</p> <p><strong>EDIT:</strong></p> <p>The earlier map function was faulty. Thats why you are not getting the results. it should have been</p> <pre><code>map = function () { emit({a:this.a, b:this.b}, {count:1}); } </code></pre> <p><strong>Test data:</strong></p> <pre><code>&gt; db.multi_group.insert({a:1,b:2}) &gt; db.multi_group.insert({a:2,b:2}) &gt; db.multi_group.insert({a:3,b:2}) &gt; db.multi_group.insert({a:1,b:2}) &gt; db.multi_group.insert({a:3,b:2}) &gt; db.multi_group.insert({a:7,b:2}) &gt; db.multi_group.mapReduce(map,reduce,{out: { inline : 1}}) { "results" : [ { "_id" : { "a" : 1, "b" : 2 }, "value" : { "count" : 2 } }, { "_id" : { "a" : 2, "b" : 2 }, "value" : { "count" : 1 } }, { "_id" : { "a" : 3, "b" : 2 }, "value" : { "count" : 2 } }, { "_id" : { "a" : 7, "b" : 2 }, "value" : { "count" : 1 } } ], "timeMillis" : 1, "counts" : { "input" : 6, "emit" : 6, "reduce" : 2, "output" : 4 }, "ok" : 1, } </code></pre> <p><strong>EDIT2:</strong></p> <p>Complete solution including applying having count >= 2</p> <pre><code>map = function () { emit({a:this.a, b:this.b}, {count:1,_id:this._id}); } reduce = function(k, values) { var result = {count: 0,_id:[]}; values.forEach(function(value) { result.count += value.count; result._id.push(value._id); }); return result; } &gt;db.multi_group.mapReduce(map,reduce,{out: { replace : "multi_result"}}) &gt; db.multi_result.find({'value.count' : {$gte : 2}}) { "_id" : { "a" : 1, "b" : 2 }, "value" : { "_id" : [ ObjectId("4f0adf2884025491024f994c"), ObjectId("4f0adf3284025491024f994f") ], "count" : 2 } } { "_id" : { "a" : 3, "b" : 2 }, "value" : { "_id" : [ ObjectId("4f0adf3084025491024f994e"), ObjectId("4f0adf3584025491024f9950") ], "count" : 2 } } </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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