0

Consider documents stored as follows and searchable as follows:

> db.MyCollection.find().sort({date_modified:-1}).limit(1).pretty()
{
    "id" : ObjectId("57b0a979034c087720261a25"),
    "date_modified" : "2016-08-11 21:58:55",
    "name" : "Mary Jane"
}

Given a date, x, such as 2016-08-01, how to query for all documents where date_modified is greater than x?

I made various attempts, such as the following. None worked:

> db.MyCollection.find({date_modified: {"$gt":new Date(2016-08-01T00:00:00.000Z)}})
2016-08-14T16:32:12.307-0500 E QUERY    [thread1] SyntaxError: identifier starts immediately after numeric literal @(shell):1:69

> db.MyCollection.find({date_modified: {"$gt":new Date(2016-08-01)}})
2016-08-14T16:32:12.307-0500 E QUERY    [thread1] SyntaxError: identifier starts immediately after numeric literal @(shell):1:69

I believe that what I need to do is first convert date_modified to a Date representation and then filter based on it. In relational databases, like MS-SQL Server, I might attempt some variation on calling CONVERT() or CAST(date_modified AS DATETIME). Is there an equivalent in MongoDB?

101010
  • 14,866
  • 30
  • 95
  • 172
  • 1
    Why wouldn't you just pass your date in using a formatted string that matches the format your dates are stored in in the db? `db.SuiteCRMAccountCache.find({date_modified: {"$gt":"2016-08-01 00:00:00"}})`? – Wake Aug 15 '16 at 01:52
  • refer to [this question](http://stackoverflow.com/questions/9314271/query-the-string-type-date-in-mongodb) , it may help you. – John Zeng Aug 15 '16 at 01:53

0 Answers0