Aggregate lookups with subarray

Hello @venkata_sreekanth_bhagavatula, Welcome to the MongoDB community Forum :slight_smile:

There are a few fixes needed in your query,

  1. $match stage in $lookup, you need to use $expr operator to match internal properties and $in operator for an array of ids,
{
    "$match": {
        "$expr": {
            "$in": ["$vid", "$$backbone_vid"]
        }
    }
}
  1. You need to use $$ROOT to access current document with $push operator in $group stage
 "$push": "$$ROOT"
  1. Change to name as property in name in $lookup from "as": "feature.vid" to "as": "features.vid"

Final Query
db.backbone.aggregate([
  {
    "$match": {
      "structureGroupIdentifier": "164"
    }
  },
  {
    "$unwind": {
      "path": "$features",
      "preserveNullAndEmptyArrays": true
    }
  },
  {
    "$lookup": {
      "from": "values_master_collection",
      "let": {
        "backbone_vid": "$features.vid"
      },
      "pipeline": [
        {
          "$match": {
            "$expr": {
              "$in": [
                "$vid",
                "$$backbone_vid"
              ]
            }
          }
        },
        {
          "$group": {
            "_id": "$vid",
            "vidLanguages": {
              "$push": "$$ROOT"
            }
          }
        }
      ],
      "as": "features.vid"
    }
  }
])
2 Likes