10

Mongo has a nice feature that tells you when a document was created.

ObjectId("53027f0adb97425bbd0cce39").getTimestamp() = ISODate("2014-02-17T21:28:42Z")

How would I get about finding all documents that were created before lets say February 10th 2014? Searched around but doesn't seem like this question comes up. Any help is appreciated! Thanks!

ndmeiri
  • 4,979
  • 12
  • 37
  • 45
Dustin
  • 6,207
  • 19
  • 61
  • 93
  • 1
    check this out: http://stackoverflow.com/questions/8749971/can-i-query-mongodb-objectid-by-date :) – JoJo Feb 25 '14 at 21:42

1 Answers1

16

You mean something like this?

db.YOUR_COLLECTION.find({YOUR_DATE_FIELD: { "$lt": ISODate("2014-02-10") }})

Guess that you have to make the same as JoJo recommended:

  1. Convert a date to an ObjectId
  2. Filter ID using $lt and returned ObjectId

Using pymongo you can do something like this:

gen_time = datetime.datetime(2014, 2, 10)
dummy_id = ObjectId.from_datetime(gen_time)
result   = collection.find({"_id": {"$lt": dummy_id}})

Reference: http://api.mongodb.org/python/1.7/api/pymongo/objectid.html

briba
  • 2,857
  • 2
  • 31
  • 59