【发布时间】:2018-12-12 10:21:28
【问题描述】:
我正在尝试从组中获得计数的总和,匹配。我怎么能得到相同的。 我有这个代码...
VisitorCompany.aggregate(
[
{
$match: {
$and:[
{ entry_date: { $gt: start, $lt: end } }
]
}
},
{
$group:
{
_id:
{
day: { $dayOfMonth: "$entry_date" },
month: { $month: "$entry_date" },
year: { $year: "$entry_date" }
},
count: { $sum:1 },
entry_date: { $first: "$entry_date" }
}
},
{
$project:
{
entry_date:
{
$dateToString: { format: "%Y-%m-%d", date: "$entry_date" }
},
count: 1,
_id: 0
}
},
{ $sort : { entry_date : -1 } },
])
输出是...
{
"count": 2,
"entry_date": "2018-12-12"
},
{
"count": 1
"entry_date": "2018-12-11"
}
有没有人知道如何获得计数的总和,即 3 (2+1),表示组之前的记录总数。提前致谢。
【问题讨论】:
-
请将示例数据以 JSON 格式发布
-
@ManjeetThakur,我认为没有必要,但这里是示例
-
{ "_id": { "$oid": "xxxxxxxxx" }, "company_id": "xxxxxxxxxxxxx", "ip_address": "103.231.89.108", "country": "Australia", “大陆”:“OC”,“visit_date”:“2018-12-12”,“entry_date”:{“$date”:“2018-12-12T09:05:03.996Z”},“__v”:0}
-
请检查日期约束 ` { $group: { _id: { day: { $dayOfMonth: "$entry_date" }, month: { $month: "$entry_date" }, year: { $年份:“$entry_date”}},计数:{$sum:1},entry_date:{$first:“$entry_date”}}},`
-
@ManjeetThakur,我想得到计数的总和,我只得到结果或获取的记录总数。查看输出
标签: mongodb mongodb-query aggregate