Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query the string type Date in mongodb

Tags:

mongodb

This is my query in mongodb

db.order.find ({"ublExtensions.ublExtensions.extensionContent.Metadata
                .ModificationTime": "2012-02-04T01:58:27.861Z"});

It is fetching the result only for the particular time zone. The modificationTime is changing on a daily basis. I have a requirement where I need to pick the data which are updated y'day...means (sysdate -1 in sql) one day data.

How can I achieve this in mongodb? Please note the my Date is a String Type and I have a constraint where I can't change that. Someone suggested to use the ISODate.

Please help in resolving this issue.

like image 745
user1214124 Avatar asked May 13 '26 19:05

user1214124


1 Answers

Mongodb stores its date objects in a bson format like: {$date: 1329415205151}

If you decide to store it in a string format, then it is the client-side responsibility to filter and process this value as mongo treats it like a string. You can convert your strings to date objects by referring to this other SO question: How do I convert a property in MongoDB from text to date type?

Its been widely recommended to either store all your dates in UTC, or, a consistent timezone possibly related to the local datacenter, and then convert your date values to the proper local timezone on the client.

You can store whatever Date value you want. The value of the date and the format of the date are two separate issues. If your constraints require you to store that string-based date format in the document, it would be recommended to also store a $date object as well at the time of update.

like image 197
jdi Avatar answered May 16 '26 12:05

jdi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!