Last active
April 18, 2018 08:24
-
-
Save phnessu4/5636642 to your computer and use it in GitHub Desktop.
mongodb group by day
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* group by day | |
* @link https://gist.github.com/phnessu4/5636642 | |
* @param query document {key1:123,key2:456} | |
*/ | |
var count_by_day = function(query){ | |
return db.action.group( | |
{ | |
keyf: function(doc) { | |
var date = new Date(doc.time); | |
var dateKey = (date.getMonth()+1)+"/"+date.getDate()+"/"+date.getFullYear(); | |
return {'date': dateKey}; | |
}, | |
cond:query, | |
initial: {count:0}, | |
reduce: function(obj, prev) { | |
prev.count++; | |
} | |
}); | |
} | |
count_by_day({this:'is',the:'query'}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment