我有一个交易表,记录了员工所休的假期。
我需要在mongodb中实现以下sql场景。
select employee,month,year,count(distinct (holiday_type) from
transactions group by employee,month,year
我需要在Mongodb中使用聚合功能,创建的Mongo查询语句如下,但是它给出了错误的结果。
db.transactions.aggregate([
{ "$group": {
"_id": {
"Month": { "$month" : "$date" },
"Year": { "$year" : "$date" },
"employee" : "$employee",
"holiday_type" : "$holiday_type"
},
"Count_of_Transactions" : { "$sum" : 1 }
}}
]);
我在使用mongodb中的count distinct逻辑时感到困惑。有什么建议吗?
$group
操作的内存使用限制已经降低,但是你的集合可能特别大。您可以在方法参数的“options”部分中添加“allowDiskUse”来对抗这个问题,就像我在编辑中所包含的那样。另请参阅aggregate命令手册页面。 - Neil Lunn