我是mongodb的新手并且对聚合有一点了解。我想要做的是根据用户输入日期计算具有不同日期和等于月和年的集合中的记录。没有错误,但此查询没有显示。
预期产出:
"work_days": [
{
"_id": null,
"count": 5
}
]
这是我的代码。
var staff_id = req.query.staff_id;
var date = req.query.date;
var d = new Date(date);
var mm = d.getMonth() + 1;
var yy = d.getFullYear();
async.parallel({
work_days: function(callback) {
Timestamp.aggregate([
{
$project: {
year: { $year: "$createdAt" },
month: { $month: "$createdAt" }
}
},
{
$match: {
staff_id: staff_id,
month: mm,
year: yy
}
},
{
$group: {
_id : { month: { $month: "$createdAt" }, day: { $dayOfMonth: "$createdAt" }, year: { $year: "$createdAt" } }
}
},
{
$group: {
_id : null,
count: {
$sum: 1
}
}
}
]).exec(callback);
},
ave_work_hours: function(callback) {
Timestamp.count(callback);
}
}, function(err, results) {
console.log(results);
res.json(results);
});
这是我的数据库
{
"_id" : ObjectId("5a94f83c234dcb690a70de72"),
"staff_id" : NumberInt(172),
"time_in" : ISODate("2018-02-25T00:45:00.000+0000"),
"time_out" : null,
"break_start" : null,
"break_end" : null,
"break_type" : null,
"train_delay" : false,
"createdAt" : ISODate("2018-02-27T06:18:36.494+0000"),
"updatedAt" : ISODate("2018-02-27T06:18:36.494+0000"),
"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5a94f844234dcb690a70de73"),
"staff_id" : NumberInt(172),
"time_in" : null,
"time_out" : null,
"break_start" : ISODate("2018-02-25T04:02:00.000+0000"),
"break_end" : null,
"break_type" : "60m",
"train_delay" : false,
"createdAt" : ISODate("2018-02-27T06:18:44.063+0000"),
"updatedAt" : ISODate("2018-02-27T06:18:44.063+0000"),
"__v" : NumberInt(0)
}
在我的数据库中,我有相同日期和同月的许多记录。