gpt4 book ai didi

mongodb - 过滤 $lookup 结果

转载 作者:可可西里 更新时间:2023-11-01 10:42:11 26 4
gpt4 key购买 nike

我有 2 个集合(带有示例文档):

报告

{
id: "R1",
type: "xyz",
}

报告文件

{
id: "F1",
reportid: "R1",
time: ISODate("2016-06-13T14:20:25.812Z")
},
{
id: "F14",
reportid: "R1",
time: ISODate("2016-06-15T09:20:29.809Z")
}

如您所见,一个报告可能有多个报告文件

我想执行一个查询,匹配一个报告 id,按原样返返回告文档,加上一个附加键存储作为子文档的 reportfile最近的 time(如果没有 reportid 会更好,因为它会是多余的),例如

{
id: "R1",
type: "xyz",
reportfile: {
id: "F14",
reportid: "R1",
time: ISODate("2016-06-15T09:20:29.809Z")
}
}

我的问题是每个报告类型都有自己的一组属性,因此在聚合管道中使用 $project 不是最好的方法。

到此为止

db.reports.aggregate([{
$match : 'R1'
}, {
$lookup : {
from : 'reportfiles',
localField : 'id',
foreignField : 'reportid',
as : 'reportfile'
}
}
])

当然会以“reportfile”的形式返回具有给定 reportid 的所有文件的列表。我如何有效地过滤该列表以获得我需要的唯一元素?

高效 -> 我尝试使用 $unwind 作为下一个管道步骤,但生成的文档长得吓人而且毫无意义。

提前感谢您的任何建议!

最佳答案

您需要添加另一个 $project$lookup 之后阶段到您的聚合管道阶段。

{ "$project": { 
"id": "R1",
"type": "xyz",
"reportfile": {
"$let": {
"vars": {
"obj": {
"$arrayElemAt": [
{ "$filter": {
"input": "$reportfile",
"as": "report",
"cond": { "$eq": [ "$$report.time", { "$max": "$reportfile.time" } ] }
}},
0
]
}
},
"in": { "id": "$$obj.id", "time": "$$obj.time" }
}
}
}}

$filter运算符“过滤”$lookup 结果并返回包含满足条件的文档的数组。这里的条件是$eq当文档有 $max 时返回真最小值。

$arrayElemAt运算符 slice $filter 的结果并返回数组中的元素,然后使用 $let 将其分配给变量运算符(operator)。从那里,您可以使用 dot notation 轻松访问结果中所需的字段。 .

关于mongodb - 过滤 $lookup 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37833808/

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