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

134
Views
Mongoose aggregate doesn't return unmatched subdocuments

I have subdocuments inside documents. And I'm trying to get entire documents if any subdocument provides multiple conditions. It's working right except not showing unmatched subdocuments.

# EXAMPLE DOCUMENT
name: "Sam",
specs: [
  {city: "istanbul", color: "blue"},
  {city: "istanbul", color: "black"},
  {city: "izmir", color: "yellow"}
]
# QUERY
Dudes.aggregate([
    {$unwind: '$specs'},
    {$match: {'specs.city': "istanbul", 'specs.color': "yellow"} }
  ]);

It's returning the matching documents but in documents there is only matched subdocument.

# OUTPUT:
name: "Sam",
specs: [
  {city: "izmir", color: "yellow"}
]
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Updated Demo - https://mongoplayground.net/p/J1GF0ErYNEk

Use $elemMatch to match object inside the array element.

db.collection.find({
  specs: {
    $elemMatch: {
      "city": "istanbul",
      "color": "blue"
    }
  }
});

Demo - https://mongoplayground.net/p/Wr1L6i2dipP

You can directly use $match and remove {$unwind: '$specs'}

db.collection.aggregate([
  {
    $match: {
      "specs.city": "istanbul",
      "specs.color": "blue"
    }
  }
]);
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!