0

I have a MongoDB with 380k documents in it and I want to delete all documents which are created before specific date. Can anyone help me with writing query for this task? Thank you.

SunCode
  • 43
  • 10
  • [the answers here](https://stackoverflow.com/questions/7327296/how-do-i-extract-the-created-date-out-of-a-mongo-objectid) might help – kmdreko Aug 22 '18 at 08:12

1 Answers1

1

The ObjectId of the document is basically contains timestamp. So you can easily construct ObjectId from timestamp and use $lt operator just like this (python code):

from bson.objectid import ObjectId
ts = datetime.datetime(2017, 1, 1)
doc_id = ObjectId.from_datetime(ts)
result = collection.find({"_id": {"$lt": doc_id}})
Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23