这是我的代码使用pymongo删除一堆记录

ids = []
with MongoClient(MONGODB_HOST) as connection:
    db = connection[MONGODB_NAME]
    collection = db[MONGODN_COLLECTION]
    for obj in collection.find({"date": {"$gt": "2012-12-15"}}):
        ids.append(obj["_id"])
    for id in ids:
        print id
        collection.remove({"_id":ObjectId(id)})

是否有删除这些记录的更好方法?就像直接删除整套记录一样
collection.find({"date": {"$gt": "2012-12-15"}}).delete() or remove()

或从obj中删除,例如
 obj.delete() or obj.remove()

或类似的东西?

最佳答案

您可以使用以下内容:

collection.remove({"date": {"$gt": "2012-12-15"}})

关于python - pymongo : delete records elegantly,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13960959/

10-16 02:19