mongodb - unix时间戳列时如何按日期对文档进行分组?
问题描述
我在 mongodb 中有以下集合
[
{
"createdAt": 1596700221742,
"id": "5f2bb63da9babddd420d0fa6",
"quoteId": "SBA0005",
"commission": 0,
},
{
"createdAt": 1596699868976,
"id": "5f2bb4dcbcf1acdbb64137d0",
"quoteId": "SBA0004",
"commission": 0,
}]
我们如何按日期分组?
解决方案
试试这个:
db.collection.aggregate([
{
$group: {
_id: {
y: { $year: { $toDate: "$createdAt" }},
m: { $month: { $toDate: "$createdAt" }},
d: { $dayOfMonth: { $toDate: "$createdAt" }}
}
}
}
])
它很可能不会给出您正在寻找的结果。但是,根据您的输入,这就是可以作为解决方案提供的全部内容。
推荐阅读
- javascript - 我需要在不更改格式的情况下链接嵌套函数的帮助
- java - HashMap求和算法
- .net - 项目构建但 Omnisharp 没有看到 nuget 包并显示错误
- c# - 如何为大型项目创建集成测试
- javascript - Vue v-针对 JSON 文件中每条记录的出现
- swiftui - 为什么@Binding 属性导致每次都没有关联初始化?
- subtitle - 如何将 SRT(基于文本的字幕)转换为 DVB(基于位图的字幕)?
- google-apps-script - 检查特定文本的所有匹配列并应用时间戳
- java - 有没有办法在服务器上一次将所有 jsp 文件转换为静态页面?
- html - 尝试在标题中显示徽标