mongodb - 如何对列上的数据进行分组并在 MongoDB 上每小时汇总记录?
问题描述
我有一个集合如下
"InteractionId": "1e4ab424-e3ab-4cc1-8642-c37bb6248a09",
"ArticleType": "Article",
"TransactionDate": ISODate("2019-06-27T16:44:16.307+05:30")
请注意,“ArticleType”可以是不同的类型,现在我想获取 ArticleType 的每小时计数。
我已经尝试了一些查询,但没有奏效。
USER_INTERACTIONS1.aggregate([
{
$project: {
_id: 0,
ArticleType: "$ArticleType",
datePartDay: {
$concat: [
{ $substr: [{ $dayOfMonth: "$TransactionDate" }, 0, 2] },
"-",
{ $substr: [{ $month: "$TransactionDate" }, 0, 2] },
"-",
{ $substr: [{ $year: "$TransactionDate" }, 0, 4] }
]
}
}
},
{
$group: {
_id: { TransactionDate: "$datePartDay", ArticleType: "$ArticleType" },
count: { $sum: 1 }
}
},
{ $sort: { _id: 1 } }
]);
解决方案
推荐阅读
- jquery - DataTables and Bootstrap 3 Uncaught SyntaxError: Invalid or unexpected token
- reactjs - 将 redux 状态传递给调度程序
- python-3.x - 我的代码在 Windows 中工作,但在 linux 中运行的相同代码给出错误
- python - 通过将列中的值与另一列中的列表匹配来连接数据帧
- nlp - CSV 格式的数据集准备
- json - 如何将数组元素存储在本地存储中
- node.js - 如何在 Twilio Whatsapp API 中为 Azure Node.js 机器人配置端点 URL?
- ios - 将IOS App从一个账户的Testflight转移到另一个账户
- node.js - 嵌入式文档仅返回 id 并创建于
- python - 关闭 requests.Session 重要吗?