Skip to content

Instantly share code, notes, and snippets.

@stellard
Forked from csanz/dupscript.js
Created May 29, 2012 21:14
Show Gist options
  • Save stellard/2830751 to your computer and use it in GitHub Desktop.
Save stellard/2830751 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.token, 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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment