Home > database >  MongoDB - Match inside $lookup pipeline for key matching not working
MongoDB - Match inside $lookup pipeline for key matching not working

Time:05-17

I am stuck in a MongoDB query. I have two collections.

plans Collection:

{
    planName: 'TV1',
    planFeatures: {
        'AA1': true,
        'AA2 : false',
        'AA3': true,
        'AA4': true,
        'AA5': false
    }
}, {
    planName: 'TV2',
    planFeatures: {
        'AA1': true,
        'AA2 : false',
        'AA3': false,
        'AA4': true,
        'AA5': false
    }
}, ..........

planFeatures Collection

{
    key: 'AA1',
    label: 'ALPHA',
}, {
    key: 'AA2',
    label: 'BETA'
}, ..........

I am trying to aggregate the 2 collections, in such a way that a key Name of planFeatures field in plans collections should be equal to key field of planFeatures collections e.g. (key name of plans.planFeatures) == (planFeatures.key) AA1 == AA1

PaymentPlanFeatures.aggregate([    
            {
                $lookup: {
                    from: 'plans',
                    let: { 'kkey': '$key'},
                    pipeline: [
                        { $set: { 'planFeatures5': { '$objectToArray': '$planFeatures' }}},
                        {
                            $match: {
                                $expr: {
                                    $eq: [
                                       '$planFeatures5.k', '$$kkey'
                                    ]
                                }
                            }
                        }
                    ],
                    as: 'paymentplans'
                }
            }
        ])

I want my result should look like this

[
    {
        "key": "AA1",
        "label": "ALPHA",
        "paymentplans": [
            {
                "planName": "TV1",
                "planFeatures": {
                    "AA1": true,
                    "AA2": true,
                    "AA3": false
                },
                "__v": 0,
                "planFeatures5": [
                    {
                        "k": "AA1",
                        "v": true
                    }
                ]
            }
        ]
    }, ..............
]

I am stuck on this issue for a long. Can anybody help me please?

CodePudding user response:

$planFeatures5.k return array of string, you should use $in operator for $match stage in $lookup pipeline.

{
  $in: [
    "$$kkey",
    "$planFeatures5.k"
  ]
}
db.planFeatures.aggregate([
  {
    $lookup: {
      from: "plans",
      let: {
        "kkey": "$key"
      },
      pipeline: [
        {
          $set: {
            "planFeatures5": {
              "$objectToArray": "$planFeatures"
            }
          }
        },
        {
          $match: {
            $expr: {
              $in: [
                "$$kkey",
                "$planFeatures5.k"
              ]
            }
          }
        }
      ],
      as: "paymentplans"
    }
  }
])

Sample Mongo Playground

  • Related