Reputation: 23
Is it possible to do a find query on a timestamp field with a date like '2016-02-24' in MongoDB?
I need to do the same thing than in SQL:
SELECT * FROM books WHERE from_unixtime(timestamp/1000) = '2016-02-24 00:00:00'
But I don't find anything to do this :-(
Thanks for any tips.
Upvotes: 2
Views: 7467
Reputation: 21766
You can query MongoDB by Unix style timestamp using the following queries:
Example 1. Check for certain date
db.data.find({"createdOn":new Date("24 Feb 2016").getTime()/1000} )
Example 2. Check where date is less than or equal to specified date
db.data.find({"createdOn":{$lte:new Date("25 Feb 2016").getTime()/1000}} )
Example 3. Check where date is greater than or equal to specified date
db.data.find({"createdOn":{$gte:new Date("25 Feb 2016").getTime()/1000}} )
I have used this test data:
use test
db.data.save({createdOn:(new Date("24 Feb 2016").getTime()/1000)} )
db.data.save({createdOn:(new Date("31 Dec 9999").getTime()/1000)} )
Upvotes: 3