from pymongo import MongoClient
from bson.objectid import ObjectId

client = MongoClient('mongodb://localhost:27017/')
db = client['PolyTraderDB']
col = db['closed_trades']

deleted = 0
for doc in col.find({}):
    if isinstance(doc['_id'], ObjectId):
        col.delete_one({'_id': doc['_id']})
        deleted += 1

print(f"Deleted {deleted} invalid ObjectId records from closed_trades.")
