更新时间:2023-01-30 13:55:16
当前无法直接在数组对象内部进行排序,
Currently sort is not possible directly inside array objects,
您可以选择两个选项,
$sort
pipeline within match documents这里使用的是 $ lookup
,而不是简单的查找,而是可以使用"$ lookup withpipeline".
here you are using $lookup
, instead of simple lookup you can use "$lookup with pipeline".
{
$lookup: {
from: "tags",
as: "tags",
let: { id: "$_id" },
pipeline: [
{
$match: {
$expr: { $eq: ["$$id", "$tag_type"] }
}
},
{
$sort: { order: -1 }
}
]
}
},
第二种可能的解决方案:游乐场
Second possible solution: Playground