delsin
delsin

Reputation: 323

How to handle date stored as string in Mongodb?

So I have this field called task_time stored as string in Mongodb in 'YYYY-MM-DD' format (eg. '2012-12-21').

Now I need to query this collection to get the data whose task_time is within a given time interval.

The time interval is given as a pair of strings that represent start time and end time in 'YYYY-MM-DD hh:mm:ss' format (eg. '2015-12-21 16:00:00').

Is there any way to do this directly in Mongo query without bringing this task to my javascript code?

Upvotes: 7

Views: 5796

Answers (1)

Manasov Daniel
Manasov Daniel

Reputation: 1378

As I know $gte and $lt can work with strings too. The same in mongoose after this issue

items.find({
    task_time: {
        $gte: "2015-12-21 12:00:00",
        $lt: "2015-12-21 16:00:00" 
    }
})

Upvotes: 12

Related Questions