2012-12-19 39 views
18

Đây là mã của tôi để xóa một loạt các hồ sơ sử dụng pymongopymongo: xóa các bản ghi thanh lịch

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)}) 

IS Có cách nào tốt hơn để xóa những hồ sơ này? như xóa một tập hợp toàn bộ hồ sơ trực tiếp

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

hoặc xóa từ obj như

obj.delete() or obj.remove() 

hoặc somehting tương tự?

Trả lời

47

Bạn có thể sử dụng như sau:

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

nếu bạn biết id, bạn có thể chỉ đơn giản là 'collection.remove (dupId) ' – Cmag

Các vấn đề liên quan