如何使用python在mongodb中一次更新500条记录?

2024-04-19 19:37:27 发布

您现在位置:Python中文网/ 问答频道 /正文

input_values = [{"001":"john"},{"002":"Josh"}] (consider there are many dicts in this)

要更新的收藏:

{
id : ObjectId("asasas87897s89as")
"name_id": "name1",
"name": ""
}

我需要将输入值dict的键与集合的名称id匹配,并更新该值

我尝试过的代码:

    for key, value in input_values.items():
            self.client[collection].update({"name_id": key},
                                          {"$set": {"name": value}},
                                          upsert=False, multi=True)

但这会一次更新一条记录。但我需要一次处理500条记录。我用的是pymongo


Tags: keynameinidinputvalue记录john
1条回答
网友
1楼 · 发布于 2024-04-19 19:37:27

您需要使用BulkWrite方法:

The number of operations in each group cannot exceed the value of the maxWriteBatchSize of the database. As of MongoDB 3.6, this value is 100,000. This value is shown in the isMaster.maxWriteBatchSize field.

This limit prevents issues with oversized error messages. If a group exceeds this limit, the client driver divides the group into smaller groups with counts less than or equal to the value of the limit. For example, with the maxWriteBatchSize value of 100,000, if the queue consists of 200,000 operations, the driver creates 2 groups, each with 100,000 operations.

使用pymongo库,您需要将更新聚合到列表中并执行批量写入操作。下面是一些例子,让我们看看pymongo中批量操作的想法:

from pymongo import UpdateOne, MongoClient

db = MongoClient()

input_values = [{"001": "john"}, {"002": "Josh"}]

updates = []

for value in input_values:
    key = list(value.keys())[0]
    updates.append(UpdateOne({'name': key}, {'$set': {'name': value[key]}}))

db.collection.bulk_write(updates)

相关问题 更多 >