Skip to content

Instantly share code, notes, and snippets.

@hasantayyar
Created June 15, 2012 09:33
mongodb count embeded objects by an atrribute
var map = function() { emit(this.e, { notifications : this.notifications});};
var reduce = function(key, values) {
var count = 0;
values.forEach(function(docs) {
for(var doc in docs.notifications){
if(docs.notifications[doc].date>1332836355){
count+=1;
}
}
});
return { c : count};
};
db.collection1.mapReduce(map, reduce, {out: collection2});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment