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

114
Vistas
mongoose : How to find by date that start with a given input (like autocomplete : 25/01/2... , 25/01..)

i have objects stored in db like :

[
   {
      "created_at" : ISODate("2020-03-19T07:27:32.158Z"),
   },
   {
      "created_at" : ISODate("2020-03-19T12:10:19.191Z"),
   }
]

I would like to find data by created_at (that start with) like autocomplete : 25... , 25/01... , 25/01/202...

I have an input date that i can get data in the format : 25/01/2021 : dd/mm/yyyy.

the result can be 25 or 25/0 ,

in this case i need to find data that created_at : start with 25/0

it's like the method : $regex: "^" + date but with dates

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

An example of the aggregation pipeline that uses $dateToString to convert it runtime. May be quite expensive on large collections as it processes all documents on each request and cannot use an index.

db.collection.aggregate([
  {
    "$addFields": {
      "created_at_str": {
        $dateToString: {
          date: "$created_at",
          format: "%d/%m/%Y" 
        }
      }
    }
  },
  {
    "$match": {
      "created_at_str": {
        $regex: "^" + date
      }
    }
  },
  {
    "$project": {
      "created_at_str": 0
    }
  }
])

An example to play with https://mongoplayground.net/p/zXJbsUnli3L

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