Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda