Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda