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

141
Vistas
MongoDB Aggregation - query collections that satisfies all array of objects not at least one

I have collections that have dates in an array like:

datesArray: [{
 start_date: Date,
 end_date: Date
}]

I want only those collections which satisfy all elements of datesArray.

I am using it in aggregation $match operator like:

Model.aggregate([
{
  $match: {
   'datesArray.start_date': { $gte: new Date('11-01-21') },
   'datesArray.end_date': { $lte: new Date('11-30-21') }
  }
}
])

I tried with $elemMatch but it matches at least one array element. I also tried $all with $elemMatch but had no success.

Thank you

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

$map your datesArray to a boolean array by your date range matching criteria. Perform $allElementsTrue on the result to get your desired result.

inputDate1 and inputDate2 are your inputs. Feel free to update them.

db.collection.aggregate([
  {
    "$addFields": {
      "inputDate1": ISODate("2021-01-01"),
      "inputDate2": ISODate("2021-12-31")
    }
  },
  {
    "$match": {
      $expr: {
        "$allElementsTrue": [
          {
            "$map": {
              "input": "$datesArray",
              "as": "d",
              "in": {
                $and: [
                  {
                    $gte: [
                      "$$d.start_date",
                      "$inputDate1"
                    ]
                  },
                  {
                    $lte: [
                      "$$d.end_date",
                      "$inputDate2"
                    ]
                  }
                ]
              }
            }
          }
        ]
      }
    }
  }
])

Here is the Mongo playground for your reference.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could simply invert each of the critera and use $nor:

db.collection.aggregate([
  {$match: {
      $nor: [
        {"datesArray.start_date": {$lt: ISODate("2021-11-01")}},
        {"datesArray.end_date": {$gt: ISODate("2021-11-30")}}
      ]
  }}
])
about 4 years ago · Juan Pablo Isaza 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