Reputation: 3537
I have a mongo database where I'm storing a date and time in separate fields (I do not have the ability to change this setup). Googling around, I found examples of people querying dates and times by breaking fields up into 2 parts, 1 for date and 1 for time, like:
{"dateField": {"d": "06/18/2012" "t": "12:00:00 AM"}}
{"timeField": {"d": "06/18/2012" "t": "12:07:10 PM"}}
I've seen people (and have been able to myself) query the date portion:
db.collection.find({"dateField.d": {"$gte": "06/18/2012"}})
My problem is I cannot get accurate results trying the same thing with time:
db.collection.find({"timeField.t": {"$gte": "12:00:00 PM"}})
Is there a specific way I should be formatting my time data to query it? Or is there a better way for storing time data so that I can query just the time (without it being dependent on a date)?
Upvotes: 2
Views: 782
Reputation: 7590
You need to store data in a real Date format or as numbers. When you are doing those searches the comparison is using lexicography order which is not what you want. If you do want to use strings you should use one of the ISO formats where it works.
Upvotes: 2