【发布时间】:2021-03-22 02:38:59
【问题描述】:
使用 mongo 或 mongoose,如何获取当月的记录总数?
我有这个,但它给了我每个月的总数,我只想计算当月的记录数。
const genTotal = await General.aggregate([
{
$group: {
_id: {
year: { $year: "$visitDate" },
month: { $month: "$visitDate" },
},
Total: { $sum: 1 },
},
},
]);
我也试过这个:
const genTotal = await General.aggregate([
{
$group: {
_id: {
month: { $month: "$visitDate" },
},
Total: { $sum: 1 },
},
},
{
$match: { $month: 3 },
},
]);
【问题讨论】:
-
检查更新的答案