When create a new ObjectId in my node.js script using:
mongojs.ObjectId()
I get an _id like
“f5818257dd0b55ce321f87b5”
When I use:
mongojs.ObjectId(“f5818257dd0b55ce321f87b5”).getTimestamp()
I get:
“Sun Jul 10 2016 19:12:21 GMT+0200 (CEST)"
But when I use ObjectId("f5818257dd0b55ce321f87b5").getTimestamp()
in the MongoDB Shell I get:
ISODate("2100-07-10T12:23:51Z")
When I want to sort my documents by _id
with:
db.stores.find().sort({_id: -1})
The documents are returned in the wrong order because the timestamp in the ObjectId
is wrong.
How do I get mongojs to produce ObjectIds
in the right format ?
I’m really confused, can anyone help me ?
Edit: When I insert a document with mongojs i get an ObjectId like:
“30a282576f9f2c4772e69cd9”
When I get the timestamp with:
ObjectId("30a282576f9f2c4772e69cd9").getTimestamp()
It returns:
ISODate("1995-11-09T22:36:07Z")
But when I insert a document using the MongoDB Shell i get an ObjectId like:
“5782a4809f3c4cbed9f2a8a1”
When I get the timestamp from this id using:
ObjectId("5782a4809f3c4cbed9f2a8a1").getTimestamp()
I get:
ISODate("2016-07-10T19:39:44Z")
These two documents are created like 5 min apart. Why is the date in the ObjectId inserted with mongojs wrong ?