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

261
Visualizações
Query MongoDB with custom date String - javascript

I am new to NodeJS. I have a collection in MongoDB which holds the Data shown below

[{
        _id: new ObjectId("6180c67a9b414de991a24c43"),
        cusDate: '20/11/2021 03:32 AM',
        cusName: 'Akila',
        cusEmail: 'akila@gmail.com',
        __v: 0
    },
    {
        _id: new ObjectId("6180c67a9bt14de991a24c43"),
        cusDate: '02/08/2021 08:12 AM',
        cusName: 'Booth',
        cusEmail: 'booth@gmail.com',
        __v: 0
    },
    {
        _id: new ObjectId("u180c67a9b414de991a24c43"),
        cusDate: '12/07/2020 11:38 AM',
        cusName: 'Sam',
        cusEmail: 'sam@gmail.com',
        __v: 0
    }, {
        _id: new ObjectId("61y0c67a9b414de991a24c43"),
        cusDate: '22/01/2021 07:42 AM',
        cusName: 'Ram',
        cusEmail: 'ram@gmail.com',
        __v: 0
    },
]

I want to find and get the Data from Collection based on cusDate.

fromCusDate: 01/06/2020 12:00 AM
toCusDate: 01/03/2021 12:00 PM

Required Output:

[
    {
        _id: new ObjectId("u180c67a9b414de991a24c43"),
        cusDate: '12/07/2020 11:38 AM',
        cusName: 'Sam',
        cusEmail: 'sam@gmail.com',
        __v: 0
    }, {
        _id: new ObjectId("61y0c67a9b414de991a24c43"),
        cusDate: '22/01/2021 07:42 AM',
        cusName: 'Ram',
        cusEmail: 'ram@gmail.com',
        __v: 0
    },
]

My cusDate was in String datatype. The String Date Format was DD/MM/YYYY hh:mm A

Here's the Code i tried,

let getOrdersData = await collection.find({ "cusDate": { "$gte": "01/06/2020 12:00 AM", "$lt": "01/03/2021 12:00 PM" } })

But my date was in String Datatype in collection. so i can't get that data.

I don't know to how to get that data. I want to know the Mongoose query to get these data.

let getOrdersMonth = await collection.find({ "cusDate": { "$gte": "01/06/2020 12:00 AM", "$lt": "01/03/2021 12:00 PM" } })

I found this answer. stackoverflow answer. But it is a mongodb query. I need a mongoose query. I searched stackoverflow but can't find any answers. Please Help me with some solutions.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To solve this using date queries in MongoDB , you first need to have a Date type entry in your MongoDB collection. Something of the sort of createdAt,updatedAt. It's easier to query on dates using Mongo but the same logic doesnt work on strings, it has to be a Date type.

So in your model you can define another entry say newCusDate where you take the cusDate and convert it into a date using new Date(cusDate). But this needs to happen when defining the collection. And then you have bunch of queries you can use on Date using MongoDB.

let gtDate = new Date('some date string here');
let ltDate = new Date('some date string here');

let getOrdersMonth = await collection.find({
  "newCusDate": {
    "$gte": gtDate,
    "$lt": ltDate
  }
})

All of them being date objects(Data Type) is very important.

about 4 years ago · Juan Pablo Isaza 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