2

I am trying to create a Mongo query in PHP. I am working with a database created by someone else, and all the dates have been saved as strings. I want to find all the entries that were created before a certain time.

By searching online, I found that I should be able to use

array('updated_at' => array('$gt' => $access_date)))

where $access_date is a MongoDate. Currently, I am using $access = new MongoDate(strtotime($access_date)); to create a MongoDate out of a date string.

But this only seems to work if update_at was saved as a MongoDate object. As mentioned, it is saved as a string. Is there an easy way to use $gt for date strings instead of MongoDates? If not I have to do a complicated for loop, and it is getting really messy.

halfer
  • 19,824
  • 17
  • 99
  • 186
user1795370
  • 332
  • 2
  • 4
  • 18
  • 1
    If the dates are saved as strings in MongoDB then it would be much more ideal to convert them to date objects on the server and do the queries with MongoDate in your application rather than using the `$gt` operator on strings which won't be as efficient as with dates or numerical values. Consider converting the schema following answers in this [**question**](http://stackoverflow.com/a/34837902/122005). – chridam Mar 06 '16 at 12:05

0 Answers0