Converting a Date to a Mongo ObjectId
If you're looking to find documents in a collection by insertion date but don't have a created_time
field, fear not.
DateTime minDate = new DateTime().minusDays(2);
Long timestamp = minDate.getMillis() / 1000L;
String oidString = Long.toHexString(l) + "0000000000000000";
And now you can run a query like so:
ObjectId id = new ObjectId(oidString);
DBObject query = new BasicDBObject("_id",
new BasicDBObject("$gt", id));
Written by Abhinav Ajgaonkar
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Mongodb
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#