diff options
author | Antonin Kral <a.kral@bobek.cz> | 2010-01-31 08:32:52 +0100 |
---|---|---|
committer | Antonin Kral <a.kral@bobek.cz> | 2010-01-31 08:32:52 +0100 |
commit | 4eefaf421bfeddf040d96a3dafb12e09673423d7 (patch) | |
tree | cb2e5ccc7f98158894f977ff131949da36673591 /jstests/group4.js | |
download | mongodb-4eefaf421bfeddf040d96a3dafb12e09673423d7.tar.gz |
Imported Upstream version 1.3.1
Diffstat (limited to 'jstests/group4.js')
-rw-r--r-- | jstests/group4.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/jstests/group4.js b/jstests/group4.js new file mode 100644 index 0000000..e75c0d1 --- /dev/null +++ b/jstests/group4.js @@ -0,0 +1,45 @@ + +t = db.group4 +t.drop(); + +function test( c , n ){ + var x = {}; + c.forEach( + function(z){ + assert.eq( z.count , z.values.length , n + "\t" + tojson( z ) ); + } + ); +} + +t.insert({name:'bob',foo:1}) +t.insert({name:'bob',foo:2}) +t.insert({name:'alice',foo:1}) +t.insert({name:'alice',foo:3}) +t.insert({name:'fred',foo:3}) +t.insert({name:'fred',foo:4}) + +x = t.group( + { + key: {foo:1}, + initial: {count:0,values:[]}, + reduce: function (obj, prev){ + prev.count++ + prev.values.push(obj.name) + } + } +); +test( x , "A" ); + +x = t.group( + { + key: {foo:1}, + initial: {count:0}, + reduce: function (obj, prev){ + if (!prev.values) {prev.values = [];} + prev.count++; + prev.values.push(obj.name); + } + } +); +test( x , "B" ); + |