使用python实现Mongo批量操作数据

一、数据库连接

from pymongo import MongoClient

1、有密码
db = MongoClient('mongodb://ip:27017').database
db.authenticate('username',password='password')

2、无密码
db = MongoClient('mongodb://ip:27017').database

二、insert_many批量插入文档

# insert_many 用法
语法格式:insert_many(documents, ordered=True, bypass_document_validation=False)


>>> db.test.count_documents({})
0
>>> result = db.test.insert_many([{'x': i} for i in range(2)])
>>> result.inserted_ids
[ObjectId('54f113fffba522406c9cc20e'), ObjectId('54f113fffba522406c9cc20f')]
>>> db.test.count_documents({})
2


备注:
insert_many(docs, ordered=True)  # 遇到错误 break, 并且抛出异常
insert_many(docs, ordered=False) # 遇到错误 continue, 循环结束后抛出异常
# 无论 ordered 是 True 或者 False, 批量添加的时候遇到重复的, 并不会对历史数据更新

补充:

insert_one(document, bypass_document_validation=False)
replace_one(filter, replacement, upsert=False, bypass_document_validation=False, collation=None)
update_one(filter, update, upsert=False, bypass_document_validation=False, collation=None)
update_many(filter, update, upsert=False, bypass_document_validation=False, collation=None)
#  都无法实现批量插入, 如果是更新,只能更新一条数据,批量更新无法实现

三、bulk_write批量更新文档

Send a batch of write operations to the server.
Requests are passed as a list of write operation instances ( InsertOne, UpdateOne, UpdateMany, ReplaceOne, DeleteOne, or DeleteMany).

语法格式:bulk_write(requests, ordered=True, bypass_document_validation=False, session=None)


>>> from pymongo import InsertOne, DeleteOne, ReplaceOne,UpdateOne
>>> from pymongo import MongoClient
>>> db = MongoClient('mongodb://ip:27017').database
>>> db.count()
1
>>> list(db.find())
[{'_id': 'a', 'n': 'a'}]

#  UpdateOne 测试一下
>>> db.bulk_write([UpdateOne({"_id":"a"},{"$set":{"n":"aa"}}, upsert=True), UpdateOne({"_id":"b"},{"$set":{"n":"b"}}, upsert=True)])
>>> list(db.find())
[{'_id': 'a', 'n': 'aa'}, {'_id': 'b', 'n': 'b'}]
# 可以看到 记录 a 的 "n" 成功被更新为 "aa", 并且新增了一条记录 b



# ReplaceOne 测试一下
>>> db.bulk_write([ReplaceOne({"_id":"b"},{"n":"bb"}, upsert=True), ReplaceOne({"_id":"c"},{"n":"cc"}, upsert=True)])
>>> list(db.find())
[{'_id': 'a', 'n': 'aa'}, {'_id': 'b', 'n': 'bb'}, {'_id': 'c', 'n': 'cc'}]

# 总结:
# 可以看到, 记录 b 的 n 从 "b" 更新为 "bb", 并且新增了一条记录 c
# 这里需要注意的是, 如果使用 ReplaceOne, update 的值不能使用"$set"
# 注意区分 ReplaceOne 和 UpdateOne 的区别。加上$set后是更新操作. 没有$set 是覆盖操作。

四、$setOnInsert插入不存在的数据

# 以下不能实现主键存在的情况下对数据进行更新
bulk = pymongo.bulk.BulkOperationBuilder(collection,ordered=True)
for doc in docs:
    bulk.find({ "_id": doc["_id"] }).upsert().updateOne({
        "$setOnInsert": doc
    })
response = bulk.execute()

# 主键已存在时, 对数据进行更新. 主键不在表中的时候对数据进行插入操作。并且, 如果要插入的数据没有设置主键 _id, 进行普通的插入操作即可。
def insert_many(collection, docs=None, update=True):
    if not docs:
        return
    # $set 的时候, 会更新数据, setOnInsert只插入不更新
    update_key = "$set" if update else "$setOnInsert"
    bulk = BulkOperationBuilder(collection, ordered=False)
    for i in docs:
        if i["_id"]:
            bulk.find({"_id":i["_id"]}).upsert().update_one({update_key:i })
        else:
            bulk.insert(i)
    result = bulk.execute()
    modify_count = result.get("nModified")
    insert_count = result.get("nUpserted") + result.get("nInserted")

五、示例

我们这边有个需求是从excel中读取商品数据,然后进行批量更新

#!/usr/bin/env python
#-*- coding:utf-8 -*-
from pymongo import MongoClient
import pandas as pd
from pymongo import UpdateOne

#定义两个列表
raw_datas = []     #原始数据
update_datas = []  #更新后数据

#连接mongo服务器
mongo = MongoClient('mongodb://127.0.0.1').pms.product

#从excel中获取要更新的数据
df = pd.read_excel('test.xlsx')
for i in df['name']:
    raw_datas.append({'myquery':{'name':'%s' %i,'companyId':28}} )

#从数据库查出要修改的数据,并更新字段
for datas in raw_datas:
    get_data = mongo.find(datas.get('myquery'))
    for data in get_data:
        data['detail']['ownership'] = 'STORE'
        datas.update({'detail':data['detail']})

#替格式[UpdateOne({"_id":"a"},{"$set":{"n":"aa"}}, upsert=True), UpdateOne({"_id":"b"},{"$set":{"n":"b"}}, upsert=True)]
for line in raw_datas:
    update_datas.append(UpdateOne(line.get('myquery'),{'$set':{'detail':line.get('detail')}},upsert=True))

#批量更新
mongo.bulk_write(update_datas)

更新前:
在这里插入图片描述
更新后:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_37886429/article/details/93480363