$lookup
用于对同一数据库中未共享的集合执行左外部联接,以从联接的集合中筛选文档,以便在Mongo中进行处理。
{
$lookup:
{
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from" collection>,
as: <output array field>
}
}
会不会
foreignField
是的嵌套文档的字段
from
收藏?
例如,有两个集合,如下所示。
history
[{
id:'001',
history:'today worked',
child_id:'ch001'
},{
id:'002',
history:'working',
child_id:'ch004'
},{
id:'003',
history:'now working'
child_id:'ch009'
}],
childsgroup
收集
[{
groupid:'g001', name:'group1'
childs:[{
id:'ch001',
info:{name:'a'}
},{
id:'ch002',
info:{name:'a'}
}]
},{
groupid:'g002', name:'group1'
childs:[{
id:'ch004',
info:{name:'a'}
},{
id:'ch009',
info:{name:'a'}
}]
}]
所以,这个
aggregation
db.history.aggregate([
{
$lookup:
{
from: "childsgroup",
localField: "child_id",
foreignField: "childs.$.id",
as: "childinfo"
}
}
])
[{
id:'001',
history:'today worked',
child_id:'ch001',
childinfo:{
id:'001',
history:'today worked',
child_id:'ch001'
}
}, .... ]
这不可能吗?