How can I aggregate my MongoDB results by ObjectId date. Example:
Default cursor results:
cursor = [
{\'_id\': ObjectId(\'5220b974a61ad0000746c0d
The Jira Ticket pointed out by llovett has been solved, so now you can use date operators like $isoWeek
and $year
to extract this information from an ObjectId
.
Your aggregation would look something like this:
{
"$project":
{
"_id": {
"$dateFromParts" : {
"year": { "$year": "$_id"},
"month": { "$month": "$_id"},
"day": { "$dayOfMonth": "$_id"}
}
}
}
}
There is no way to accomplish what you're asking with mongodb's aggregation framework, because there is no aggregation operator that can turn ObjectId's into something date-like (there is a JIRA ticket, though). You should be able to accomplish what you want using map-reduce, however:
// map function
function domap() {
// turn ObjectId --> ISODate
var date = this._id.getTimestamp();
// format the date however you want
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
// yields date string as key, entire document as value
emit(year+"-"+month+"-"+day, this);
}
// reduce function
function doreduce(datestring, docs) {
return {"date":datestring, "docs":docs};
}
So this doesn't answer my question directly, but I did find a better way to replace all that lambda nonsense above using Python's setdefault
:
d = {}
for message in messages:
key = message['_id'].generation_time.date()
d.setdefault(key,[]).append(message)
Thanks to @raymondh for the hint in is PyCon talk:
Transforming Code into Beautiful, Idiomatic Python