1
我试图按照日期将文档与Mongo聚合框架进行分组。我尝试了一堆东西,无法识别问题。我的查询是这样的:按日期分组Mongo aggregation
// get current date and then get the date 8 months ago
var now = Date.now()
var lastMonth = 60*60*24*30*1000*8
lastMonth = now - lastMonth
var query = {
type: 'something',
deleted: {$ne: true},
created: {$gte: lastMonth}
}
var stickerNames = [];
db.Message.aggregate([{$match: query},
{$group:
{_id: {data: '$data',
month: {$month: '$created'},
year: {$year: '$created'}
},
count:{$sum: 1}
}
},
{$sort:
{count: 1}}],
function(err, stuff){
if(err){
return res.json({error: err.message})
}
else{
return res.json({data: stuff})
}
})
我试过一吨的不同的东西与日期,并检查其他SO职位,但至今没有奏效。如果我取出创建的:{$ gte:lastMonth}子句,则它返回每个类型为“something”的文档。如果我离开它,它不会返回任何文件。
创建的域模型由
created: {type: Date, default: Date.now}
一个Date对象负数返回时间戳值而不是毫秒数。你应该这样做:'lastMonth = new Date(now - lastMonth)' ' –