我想要一个等价的查询,以
SELECT YEAR(displayDate) AS YEAR,
count(_id) as Total
from collection
where Month(displayDate)=1
group by YEAR(displayDate);
如果你清楚地进入SQL查询,我想要总的 _id
算作 month=1
并按 year
这样做会得到的输出是
-----------------------
Year | Total
-----------------------
2016 | 30
2017 | 45
2018 | 60
-----------------------
尝试了同等的查询,以获得在Mongo
db.collection.aggregate(
{$match: {}},
{$group : {
_id : {$year : "$displayDate"},
count : {$sum : 1}
}})
但使用上面的mongo查询,我得到所有12个月的总数,我只想得到第1个月的,如何过滤这个在 $match
第1个月,其中 displayDate
是日期的字段.我想用SELECT YEAR(displayDate AS YEAR, count(_id) as Total from collection where Month(displayDate =1 group by YEAR(displayDate); If you .
你想使用 元月
db.collection.aggregate([
{
$match: {
$expr: {
$eq: [{$month: "$displayDate"}, 1]
}
}
},
{
$group: {
_id: {$year: "$displayDate"},
count: {$sum: 1}
}
}
])