pymongo快速批量删除,批量插入,批量更新

想使用pymongo实现批量操作,找了很久,终于在文档中发现
pymongo文档原文

from pymongo import InsertOne, DeleteOne, ReplaceOne
requests = [InsertOne({'y': 1}), DeleteOne({'x': 1}),ReplaceOne({'w': 1}, {'z': 1}, upsert=True)]
result = db.test.bulk_write(requests)

返回的result会包含此次操作的信息

>>> result.inserted_count
1
>>> result.deleted_count
1
>>> result.modified_count
0
>>> result.upserted_ids
{2: ObjectId('54f62ee28891e756a6e1abd5')}

超级好用!!!

你可能感兴趣的:(解决问题,Python,数据库)