Skip to content

Instantly share code, notes, and snippets.

@csanz
Created March 21, 2011 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save csanz/878897 to your computer and use it in GitHub Desktop.
Save csanz/878897 to your computer and use it in GitHub Desktop.
Looking for duplicates / Simple script example
db.users.group({ key: { name:true },
cond: { "created_at"},
initial: { count: 0 },
reduce: function(obj,out) { out.count ++ ; }});
/*
Note1: make sure you put the initial declaration above reduce or else the count variable never gets set
Note2: group() can't handle more than 10000 unique keys, so for large dbs you are better off using straight up map reduce.
*/
m = function () {
emit(this.name, 1);
};
r = function (k, vals) {
return Array.sum(vals);
};
/* shell
> res = db.users.mapReduce(m,r);
{
"result" : "tmp.mr.mapreduce_1300684269_8",
"timeMillis" : 22,
"counts" : {
"input" : 11,
"emit" : 11,
"output" : 5
},
"ok" : 1,
}
> db[res.result].count({ "value": { $gte: 2 }});
3
*/
db[res.result].drop();
@abhas
Copy link

abhas commented Sep 3, 2012

Thanks very much for your effort. This helped me a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment