如何按“日期”字段对“$lookup”结果数组进行排序
我正在尝试对通过 $lookup
通过名为 createdAt
的字段计算的数组(降序)进行排序,该字段包含 Date
对象:
[
{
"$lookup": {
"from": "activities",
"localField": "activities",
"foreignField": "_id",
"as": "activities"
}
},
{
"$sort": {
"activities.createdAt": -1
}
},
{
"$project": {
"_id": false,
"updatedAt": false,
"activities._id": false,
"activities.game": false,
"activities.updatedAt": false,
"activities.__v": false,
"__v": false
}
}
]
activities
字段已正确“填充”,但未对结果进行排序。
I'm trying to sort an array (descending) that gets computed through a $lookup
by a field called createdAt
which contains a Date
object:
[
{
"$lookup": {
"from": "activities",
"localField": "activities",
"foreignField": "_id",
"as": "activities"
}
},
{
"$sort": {
"activities.createdAt": -1
}
},
{
"$project": {
"_id": false,
"updatedAt": false,
"activities._id": false,
"activities.game": false,
"activities.updatedAt": false,
"activities.__v": false,
"__v": false
}
}
]
The activities
field is being "populated" correctly but it's not sorting the results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试用像波纹管这样的管道来替换查找。
如果您希望对数组进行排序,则可以在该管道中进行。
如果这不是您需要的,那么如果您可以添加一些示例数据和预期的输出。
Try to replace your lookup with a lookup with pipeline like the bellow.
If you want the array to be sorted, you can do it inside that pipeline.
If this isn't what you need if you can add some sample data and expected output.