Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda