$match + $lookup

https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/

  1. {
  2. $lookup:
  3. {
  4. from: <collection to join>,
  5. localField: <field from the input documents>,
  6. foreignField: <field from the documents of the "from" collection>,
  7. as: <output array field>
  8. }
  9. }
Field Description
from 指定另一个关联的collection
localField 指定在本collection中的字段名
foreignField 指定在关联collection中的字段名
as 指定为新的字段名

示例

  1. db.getCollection('var').aggregate([
  2. {$match: { 'Variation ID': 'RC12' } },
  3. {$lookup: {
  4. from: 'var_detail',
  5. localField: 'Variation ID',
  6. foreignField: 'Variation ID',
  7. as: 'detail' }
  8. },
  9. ])