I need to remove the data in one of the collection in dev enrionment but only remove the data that is created until yesterday.
id field documents are numeric values(like 1,2,3 etc)… i am working in mongo shell looking for help on this. There are no date fields in my json that i could use filter criteria to remove.
As this is a dev environment and I am assuming by ‘yesterday’ you mean any giving yesterday and not exactly Feb 18? Could you not just clear the database and await fresh data?
I could be wrong but I think you would need some date field stored in the documents you wish to remove by a certain date
I really do not understand. You do not have or want any field with a date in your documents yet you want to be able to delete document for a given date. But this is comparable to I want delete all Blue items but my items have no colour. Impossible
The $natural parameter returns items according to their natural order within the database. This ordering is an internal implementation feature, and you should not rely on any particular structure within it.
I think the best way is to add a created_at field or some sort of date field. Keeping in mind this is only a dev environment things should be easy to change no?
Hey @nagaraju_voora while I can do this, you really probably shouldn’t. At least not this way, I strongly agree with the previous posters that the prefered method would be to add a timestamp to your documents. That being said, you can probably do what you’ve asked using the oplog
import pymongo
from bson.objectid import ObjectId
from datetime import datetime, timedelta
client = pymongo.MongoClient()
oplog = client.local.oplog.rs
db = client['dev']
previous_day = datetime.now() - timedelta(days=1)
ops = oplog.find({"ns":"dev.users", "op": "i", "wall": {"$lt": previous_day}})
for op in list(ops):
db["users"].delete_one({"_id": ObjectId(op['o']['_id'])})
This code is incredibly hacky and I do not recommend you run it in any environment, not even dev… but as a purely academic exercise of “is it possible”, then the answer is yes.