italktothewind
italktothewind

Reputation: 2195

Mongo aggregation with $lookup not working when using let

I have documents like this in a collectionA:

{ 
    "catalogId" : "17582"
}

And documents like this in a collectionB:

{ 
    "product" : {
       "catalogId" : "17582"
}

I'm applying the following aggregation to collectionA:

{
    $lookup: {
     from: "collectionB",
     let: {
        catalogId: "$catalogId"
      },
      pipeline: [
          {
            $match: {
                "product.catalogId": "$$catalogId"
            }
          }         
      ],
      as: "something"
   }
}

But I'm having an empty result. Anyone knows why? Thanks in advance.

Upvotes: 1

Views: 93

Answers (1)

whoami - fakeFaceTrueSoul
whoami - fakeFaceTrueSoul

Reputation: 17915

Query has to be like below, check this $lookup for ref :

db.collectionA.aggregate([{
    $lookup: {
        from: "collectionB",
        let: {
            catalogId: "$catalogId"
        },
        pipeline: [
            {
                $match: {
                    $expr: { $eq: ["$product.catalogId", "$$catalogId"] }
                }
            }
        ],
        as: "something"
    }
}])

Upvotes: 2

Related Questions