$match + $lookup
https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/
{
$lookup:
{
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from" collection>,
as: <output array field>
}
}
Field | Description |
---|---|
from | 指定另一个关联的collection |
localField | 指定在本collection中的字段名 |
foreignField | 指定在关联collection中的字段名 |
as | 指定为新的字段名 |
示例
db.getCollection('var').aggregate([
{$match: { 'Variation ID': 'RC12' } },
{$lookup: {
from: 'var_detail',
localField: 'Variation ID',
foreignField: 'Variation ID',
as: 'detail' }
},
])