【发布时间】:2021-05-24 19:55:56
【问题描述】:
我有两个多对一关系的集合(多个主机的 http 服务通常提供“相同”服务,例如 DNS 级负载平衡)。 我正在尝试构建一个查询,返回合并为一个的相关文档(来自两个集合)。
主机集合:
{
"_id" : ObjectId("60aa2485332483cb4f5e7122"),
"ip" : "1.2.3.4",
"services" : [
{
"proto" : "tcp",
"port" : "22",
"status" : "open",
"reason" : "syn-ack",
"ttl" : 53,
},
{
"proto" : "tcp",
"port" : "80",
"status" : "open",
"reason" : "syn-ack",
"ttl" : 51,
"http" : [
ObjectId("60aa64c67d0bf23ce47c530c")
]
}
],
"version" : 4,
"last_scanned" : 1621573240.730579,
https 合集:
{
"_id" : ObjectId("60aa64c67d0bf23ce47c530c"),
"vhost" : "test.com",
"paths" : [
{
"path" : "/admin",
"code" : 200
},
{
"path" : "/stuff",
"code" : 200
}
]
}
我想编写一个查找,其中输出是这两个集合的组合。到目前为止,我能够将 https 文档放入主机中的顶级数组中:
db.hosts.aggregate([
{
$lookup:
{
from: "https",
localField: "services.http",
foreignField: "_id",
as: 'http'
}
}
]).pretty()
最终结果为:
{
"_id" : ObjectId("60aa2485332483cb4f5e7122"),
"ip" : "1.2.3.4",
"services" : [
{
"proto" : "tcp",
"port" : "22",
"status" : "open",
"reason" : "syn-ack",
"ttl" : 53,
},
{
"proto" : "tcp",
"port" : "80",
"status" : "open",
"reason" : "syn-ack",
"ttl" : 51,
"http" : [
ObjectId("60aa64c67d0bf23ce47c530c")
]
}
],
"http" : [
{
"_id" : ObjectId("60aa64c67d0bf23ce47c530c"),
"vhost" : "test.com",
"paths" : [
{
"path" : "/admin",
"code" : 200
},
{
"path" : "/stuff",
"code" : 200
}
]
}
]
"version" : 4,
"last_scanned" : 1621573240.730579
]
}
问题是我无法将“http”字段移动到通过查找 (services.$.http) 找到它的 ObjectId 的位置。我试图以各种方式修改 $lookup 的“as”字段,但没有成功。
甚至可以用“as”指向嵌套文档的较低级别吗? 有什么解决方法可以实现这一点?
【问题讨论】:
标签: mongodb aggregation-framework