Home > Back-end >  Query MongoDB with custom date String - javascript
Query MongoDB with custom date String - javascript

Time:11-09

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: '[email protected]',
        __v: 0
    },
    {
        _id: new ObjectId("6180c67a9bt14de991a24c43"),
        cusDate: '02/08/2021 08:12 AM',
        cusName: 'Booth',
        cusEmail: '[email protected]',
        __v: 0
    },
    {
        _id: new ObjectId("u180c67a9b414de991a24c43"),
        cusDate: '12/07/2020 11:38 AM',
        cusName: 'Sam',
        cusEmail: '[email protected]',
        __v: 0
    }, {
        _id: new ObjectId("61y0c67a9b414de991a24c43"),
        cusDate: '22/01/2021 07:42 AM',
        cusName: 'Ram',
        cusEmail: '[email protected]',
        __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: '[email protected]',
        __v: 0
    }, {
        _id: new ObjectId("61y0c67a9b414de991a24c43"),
        cusDate: '22/01/2021 07:42 AM',
        cusName: 'Ram',
        cusEmail: '[email protected]',
        __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.

CodePudding user response:

Use new Date like this

 new Date(Date.parse(“01/03/2021 12:00 PM“))

CodePudding user response:

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
  }
})
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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

  • Related