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?