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

74
Vistas
MongoDB Atlas multiple range search

Is there a way to search multiple ranges for collections in mongo database?. I have tried the following but doesn't work although it works for a single range

db.collection(collection)
  .aggregate([
    {
      $search: {
        compound: {
          filter: [
            {
              range: {
                path: createdAt,
                gte: startdate,
                lte: endDate,
              },
            },
            {
              range: {
                path: updatedAt,
                gte: startdate,
                lte: endDate,
              },
            },
          ],
        },
      },
    },
    {
      $limit:20,
    },
  ])
  .toArray()
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You may simply put a $or in $expr in the $match stage of your aggregation

db.collection.aggregate([
  {
    "$match": {
      $expr: {
        $or: [
          {
            $and: [
              {
                $gte: [
                  "$createdAt",
                  startDate
                ]
              },
              {
                $lte: [
                  "$createdAt",
                  endDate
                ]
              }
            ]
          },
          {
            $and: [
              {
                $gte: [
                  "$updatedAt",
                  startDate
                ]
              },
              {
                $lte: [
                  "$updatedAt",
                  endDate
                ]
              }
            ]
          }
        ]
      }
    }
  }
])

Here is the Mongo playground for your reference.

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