How to delete data in MongoDB by create date? [duplicate]
How to delete data in MongoDB by create date? [duplicate]
This question already has an answer here:
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.
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1 Answer
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):
ObjectId
ObjectId
$lt
from bson.objectid import ObjectId
ts = datetime.datetime(2017, 1, 1)
doc_id = ObjectId.from_datetime(ts)
result = collection.find("_id": "$lt": doc_id)
the answers here might help
– kmdreko
Aug 22 at 8:12