{
{
"date": "2017-09-04",
"description": "DD from my employer1",
"amount": 1000.33
},
{
"date": "2017-09-06",
"description": "DD from my employer1",
"amount": 1000.34
},
{
"date": "2017-09-06",
"description": "DD from my employer1",
"amount": 1000.35
},
{
"date": "2017-09-07",
"description": "DD from employer1",
"amount": 5000.00
},
{
"date": "2017-09-08",
"description": "DD from my employer1",
"amount": 2000.33
},
{
"date": "2017-09-09",
"description": "DD from my employer1",
"amount": 2000.33
},
{
"date": "2017-09-10",
"description": "DD from my employer1",
"amount": 2000.33
}
}
我上面有这组对象,我正在尝试计算唯一日期的数量。有7个具有日期字段的对象,但正如您所见,2017-09-06显示两次,这意味着只有6天的总计数。我正在尝试对我的约会进行分组,但是不能让mongo只计算一次日期,并将那些只出现一次的对象视为一个。
我试过这个......
{
{
"$group": {
"_id": {
"description": "$description",
"date": "$date"
},
"count": {
"$sum": 1
}
}
}
}
答案 0 :(得分:1)
使用聚合:
db.collection.aggregate(
// Pipeline
[
{
$group: {
_id: "$date"
}
},
{
$group: {
_id:1,
count: {$sum : 1 }
}
},
]
)
答案 1 :(得分:0)
使用distinct来获取唯一值
db.getCollection('books').distinct('date')
如果您想要唯一值的总数
db.getCollection('books').distinct('date').length