I don't seem to be able to get even the most basic date query to work in MongoDB. With a document that looks something like this:
{
"_id" : "foobar/201310",
"ap" : "foobar",
"dt" : ISODate("2013-10-01T00:00:00.000Z"),
"tl" : 375439
}
And a query that looks like this:
{
"dt" : {
"$gte" : {
"$date" : "2013-10-01T00:00:00.000Z"
}
}
}
I get 0 results from executing:
db.mycollection.find({
"dt" : { "$gte" : { "$date" : "2013-10-01T00:00:00.000Z"}}
})
Any idea why this doesn't work?
For reference, this query is being produced by Spring's MongoTemplate so I don't have direct control over the query that is ultimately sent to MongoDB.
(P.S.)
> db.version()
2.4.7
Thanks!
In the MongoDB shell:
In my nodeJS code ( using Mongoose )
I am querying my sensor events collection to return values where the 'from' field is greater than the given date
This worked for me while searching for value less than or equal than now:
Although
$date
is a part of MongoDB Extended JSON and that's what you get as default withmongoexport
I don't think you can really use it as a part of the query.If try exact search with
$date
like below:you'll get error:
Try this:
or (following comments by @user3805045):
ISODate
may be also required to compare dates without time (noted by @MattMolnar).According to Data Types in the mongo Shell both should be equivalent:
and using
ISODate
should still return a Date object.{"$date": "ISO-8601 string"}
can be used when strict JSON representation is required. One possible example is Hadoop connector.From the MongoDB cookbook page comments:
This worked for me. In full context, the following command gets every record where the
dt
date field has a date on 2013-10-01 (YYYY-MM-DD) Zulu:In json strict mode, you'll have to keep the order:
Only thing which worked to define my search queries on mlab.com.
this is my document
now i want to find every 27th date document.so i used this....
this worked for me.