Skip to content

Instantly share code, notes, and snippets.

@hasantayyar
Created March 27, 2012 08:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasantayyar/2213970 to your computer and use it in GitHub Desktop.
Save hasantayyar/2213970 to your computer and use it in GitHub Desktop.
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