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

267
Vistas
Mongodb, Verify if my query range is not overlapping a list of Date Ranges in mongodb

I have the following data:

const Availabilities = new Schema({
    id: Number,
    reservations: [{ from: Date, to: Date }]
});
const Availability = mongoose.model('Availability', Availabilities);

Basically i want to get a list of products in which the dates do not overlap with the query

[
    {
        id: 1,
        reservations: [
            {
                from: '2022-01-01',
                to: '2022-03-31',
            },
            {
                from: '2022-04-01',
                to: '2022-06-01',
            },
        ],
    },
    {
        id: 2,
        reservations: [
            {
                from: '2022-01-01',
                to: '2022-12-31',
            },
        ],
    },
    {
        id: 3,
        reservations: [
            {
                from: '2022-02-01',
                to: '2022-06-30',
            },
        ],
    },
]

and I want to filter all those who are not in a specific range. Example:

query = { from: "2022-07-01", to: "2022-08-31" }

should return

[
    {
        id: 1,
        reservations: [
            {
                from: '2022-01-01',
                to: '2022-03-31',
            },
            {
                from: '2022-04-01',
                to: '2022-06-01',
            },
        ],
    },
    {
        id: 3,
        reservations: [
            {
                from: '2022-02-01',
                to: '2022-06-30',
            },
        ],
    },
]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can just check if one of two conditions are met, either:

  1. to is smaller than the start of your input ( meaning the session ended before )
  2. from is bigger than the end of your input ( meaning the session started after )

This is how it'll look in code:

db.collection.aggregate([
  {
    $match: {
      reservations: {
        $elemMatch: {
          $or: [
            {
              to: {
                $lt: "2022-07-01"
              },
              
            },
            {
              from: {
                $gt: "2022-08-31"
              }
            }
          ]
        }
      }
    }
  },
  {
    $addFields: {
      reservations: {
        $filter: {
          input: "$reservations",
          cond: {
            $or: [
              {
                $lt: [
                  "$$this.to",
                  "2022-07-01"
                ]
              },
              {
                $gt: [
                  "$$this.to",
                  "2022-08-31"
                ]
              }
            ]
          }
        }
      }
    }
  }
])

Mongo Playground

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