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.
Asked
Active
Viewed 1,157 times
1 Answers
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