Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!