Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
$in/$eq in mongo pipeline $lookup is not working

I've a collection details with array of string values on quotes_id like the following

{
    "quantity": 2000,
    "quotes_id": ["SQ-CPQ10037"],
    "status": "processed",
    "logs": [""],
    "product_id": "5ff5cf3dc1ceb9144901da81"
}

And my quotes collection

{
    "_id":"SQ-CPQ10037",
    "product_id":"5ff5cf3dc1ceb9144901da81",
    ...more fields here
}

Now, I'm trying to access this collection from another collection named quotes using lookup pipeline like the following

$lookup: {
     from: "details",
     "let": { "product_id": "$quotes.product_id", "quotes_id": "$quotes._id"},
     pipeline: [
     { 
         $match: { 
            $expr: { 
               $and: [
                 { $in: [ "$quotes_id",  ["$$quotes_id"] ] },
                 { $eq: [ "$product_id",  "$$product_id" ] },
               ]
            }
         }
     }],      
     as: "product.productquotes",
},

This is returning empty collection. However, if I comment the line

{ $in: [ "$quotes_id",  ["$$quotes_id"] ] },

then it is returning me the record. I also tried with simple $eq for the quotes_id but with same result. But if I run the query directly like

{$and: [{"product_id": ObjectId('5ff5cf3dc1ceb9144901da81')}, {"quotes_id": "SQ-CPQ10037"}]}

on mongo compass, I receive the record without any issue.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You have done almost correct. But wrongly wrote the $in args. Second arg of in should be an array.

{
  $in: [
    "$$quotes_id",
    "$quotes_id"
  ]
}

So the lookup likes

db.quotes.aggregate([
  {
    $lookup: {
      from: "details",
      "let": { "product_id": "$product_id", "quotes_id": "$_id" },
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                {  $in: [ "$$quotes_id", "$quotes_id" ] },
                {  $eq: [ "$product_id", "$$product_id" ] }                
              ]
            }
          }
        }
      ],
      as: "product.productquotes"      
    }
  }
])

Working Mongo playground

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!