pymongo批量增删改查
from mongoengine.connection import get_db, connect from pymongo import MongoClient, ASCENDING, UpdateOne, InsertOne, DeleteOne, ReplaceOne from pymongo.errors import BulkWriteError # pymongo建立索引 client = MongoClient('120.xx.xx.xx:20002', username='xxx', password='xxxx') db = client.annosys image_db = db['image'] image_db.create_index([('image_id', ASCENDING)], unique=True) image_index = sorted(list(image_db.index_information())) print(image_index) # pymongo批量插入和批量更新 mdb = MongoClient('120.xx.xx.xx:20002', username='xxx', password='xxxx') db_name = mdb['db_name'] try: lines = [此处填写数据列表] image_batch = [] pick_batch = [] for idx, line in enumerate(lines): image_batch.append(UpdateOne( {'image_id': line.get("id")}, { '$set': { 'url': line.get("url"), 'width': line.get("width"), 'height': line.get("height") }, '$addToSet': { 'datasets': line.get("dataset_id") } }, upsert=True )) pick_batch.append(InsertOne( {'task_id': line.get("task_id"), 'image_id': line.get("id")} )) if idx % 2000 == 0: try: db_name['image'].bulk_write(image_batch) image_batch = [] db_name['image_pick'].bulk_write(pick_batch) pick_batch = [] except BulkWriteError as bwe: print("mongo error:", bwe.details) db_name['image'].bulk_write(image_batch) db_name['image_pick'].bulk_write(pick_batch) except Exception as e: print("Exception:", e) # pymongo批量查询 brand_images = mdb['goodlook']['brand_images'].find({"image_id": {"$in": image_ids}}) #image_ids是图片信息列表 # pymongo批量删除 brand_images = mdb['goodlook']['brand_images'].find({"image_id": {"$in": image_ids}}) mdb['goodlook']['brand_images'].remove({"image_id": {"$in": image_ids}}) # pymongo批量杂糅操作 requests = [InsertOne({'y': 1}), DeleteOne({'x': 1}),ReplaceOne({'w': 1}, {'z': 1}, upsert=True)] result = db.test.bulk_write(requests)