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

66
Vistas
How to find documents that their data match a condition on at least one item from a given array of items - mongoDB

I want to fetch records that exists in provided dates array.

This provided array of dates will fetch records between start & end date.

Example:

I have this schema:

[
    {
        "_id": "1",
        "course": "Maths",
        "startDate": "2022-06-07",
        "endDate": "2022-06-12"
    },
    {
        "_id": "2",
        "course": "Chemistry",
        "startDate": "2022-06-06",
        "endDate": "2022-06-09"
    },
    {
        "_id": "3",
        "course": "Physics",
        "startDate": "2022-06-08",
        "endDate": "2022-06-10"
    },
    {
        "_id": "4",
        "course": "Computer Science",
        "startDate": "2022-06-10",
        "endDate": "2022-06-18"
    },
    {
        "_id": "5",
        "course": "Computer Science",
        "startDate": "2022-06-10",
        "endDate": "2022-06-13"
    },
    {
        "_id": "6",
        "course": "Biology",
        "startDate": "2022-06-08",
        "endDate": "2022-06-10"
    }
]

Provided array:

["2022-06-06", "2022-06-17"]

The expected response is:

[
    {
        "_id": "2",
        "course": "Chemistry",
        "startDate": "2022-06-06",
        "endDate": "2022-06-09"
    },
    {
        "_id": "4",
        "course": "Computer Science",
        "startDate": "2022-06-10",
        "endDate": "2022-06-18"
    }
]

Can anyone help me with the query? Thanks

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

0

You can use an aggregation pipeline with $filter:

  1. Add the array of dates to the documents
  2. Add a matchingDates field that counts the items in the dates array that matches the condition.
  3. $match only documents that have such items.
  4. Format
db.collection.aggregate([
  {$addFields: {dates: ["2022-06-06", "2022-06-17"]}},
  {
    $set: {
      matchingDates: {
        $size: {
          $filter: {
            input: "$dates",
            as: "item",
            cond: {
              $and: [
                {$gte: [{$toDate: "$$item"}, {$toDate: "$startDate"}]},
                {$lte: [{$toDate: "$$item"}, {$toDate: "$endDate"}]}
              ]
            }
          }
        }
      }
    }
  },
  {$match: {matchingDates: {$gt: 0}}},
  {$unset: ["dates", "matchingDates"]}
])

Playground example

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