Reputation: 6678
I have adopted spring-data-mongo
for a while now but I hit a blocker when I need to fetch
data regularly ,say every 20 seconds
. I use ISODate Object so it's of the form ISODate("2013-08-09T05:51:16.140Z")
.
Below is a sample document
{
"_id": "fffd8e68-e81a-4835-ac5c-ef532291584b",
"_class": "net.publichealth.edowmis.datalayer.models.Post",
"userID": "09258b84-6d09-4977-a0f1-4e59b963221f",
"dateCreated": ISODate("2013-08-09T06:29:07.413Z"),
"lastModified": ISODate("2013-08-09T06:29:07.413Z"),
}
I would like to query the database to find fetch all entries at the time : 2013-08-09 06:29:07
but non of the queries below worked. I have tried these queries in mongo shell
(ubuntu terminal) and rockmongo
and intellij mongo
plugin
// rock mongo on post collection
{
"dateCreated" : new ISODate("2013-08-09T06:29:07")
}
{
"dateCreated" : new ISODate("2013-08-09 06:29:07")
}
{
"dateCreated" : new ISODate("2013-08-09")
}
// terminal and intelliJ
db.post.find({"dateCreated":new ISODate("2013-08-09T06:29:07")}) // did all the combination above.
No Result :(
I can't find the catch. Kindly have a look at it for me
Thank you
Upvotes: 0
Views: 1867
Reputation: 241525
First, don't forget the Z
. That is the ISO8601 indicator for "zulu time", which is another way of saying "UTC" or "GMT". Without it, you are just representing an "unspecified" time, or in other words - a calendar position that is not a distinct moment in time because you didn't indicate how it relates to UTC. (I am not sure if Mongo will allow those or not.)
Secondly, it looks like you are trying do an exact equality match. The value in the database has decimals, but the value you're querying does not. So they will never match because they are not the same value.
You might instead want to do a range query. For example, you might do this:
db.post.find({"dateCreated": {"$gte": new ISODate("2013-08-09T06:29:07Z"),
"$lt": new ISODate("2013-08-09T06:29:08Z")}})
See also this documentation.
Upvotes: 3