gpt4 book ai didi

MongoDB 聚合错误 : expression must have exactly one field

转载 作者:行者123 更新时间:2023-12-04 14:14:27 25 4
gpt4 key购买 nike

我有以下收藏:

{
"_id" : ObjectId("5c3f744a0f05e90001e0085a"),
"date" : ISODate("2016-01-12T12:00:00.779Z"),
}

我需要 我的文件来自 所以我想出了这个解决方案:
db.collection.aggregate([
{
$group : {
_id: { $year: { date: '$date' }, $week: { date: '$date' } },
date: { $first: '$date' }
}
},
{
$sort: { date: -1 }
}
]);

但是当我运行它时,我收到此错误:

表示表达式的对象必须只有一个字段:{ $year: { date: "$date"}, $week: { date: "$date"} }

如何解决这个问题或者有什么更好的解决方案?
谢谢

最佳答案

解决了命名表达式的问题:

db.collection.aggregate([
{
$group : {
_id: {
year: { $year: { date: '$date' } },
week: { $week: { date: '$date' } }
},
date: { $first: '$date' }
}
},
{
$sort: { date: -1 }
}
]);

关于MongoDB 聚合错误 : expression must have exactly one field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61820098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com