The Date
constructor expects a timestamp in milliseconds. The 1285947037
timestamp is in seconds, which is why you're multiplying it by 1000.
My guess is that the actual timestamp in the document contains milliseconds, e.g. 1285947037461
(note the 461 at the end). You're multiplying the seconds by 1000, which results in 1285947037000
. As you can see, these timestamp aren't equal:
1285947037461 // actual timestamp in MongoDB
1285947037000 // value you calculated
The problem lies with the MongoDate
class: it loses the milliseconds precision, as you can read in the documentation. Here's the quote:
[...] this means that any precision beyond milliseconds will be lost when the document is sent to/from the database.
To find the document you're looking for using the timestamp in seconds, you need something like this:
db.foo.findOne(function () {
// round the stored date to seconds and compare to timestamp
return parseInt(this.date / 1000) === 1285947037
})
You can get the exact timestamp in milliseconds in the console, by using myDateObject.getTime()
.