使用pymongo的bulk_write批量更新不同条件的数据

​ 我们知道可以使用update根据查询条件进行批量更新MongoDB中的数据,但是这种更新的查询条件往往具有单一性,如果我们想一次性提交不同的查询条件进行不同的更新的这个时候update就不能满足了。

​ 多方查找之后发现了 bulk_write 这个函数,下面我们看看如何使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
>>> for doc in db.test.find({}):
... print(doc)
...
{u'x': 1, u'_id': ObjectId('54f62e60fba5226811f634ef')}
{u'x': 1, u'_id': ObjectId('54f62e60fba5226811f634f0')}
>>> # DeleteMany, UpdateOne, and UpdateMany are also available.
...

>>> 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.inserted_count
1
>>> result.deleted_count
1
>>> result.modified_count
0
>>> result.upserted_ids
{2: ObjectId('54f62ee28891e756a6e1abd5')}
>>> for doc in db.test.find({}):
... print(doc)
...
{u'x': 1, u'_id': ObjectId('54f62e60fba5226811f634f0')}
{u'y': 1, u'_id': ObjectId('54f62ee2fba5226811f634f1')}
{u'z': 1, u'_id': ObjectId('54f62ee28891e756a6e1abd5')}

我们看到可以一次性提交插入、删除、更新操作,并且可以从返回值中获取到一些新。

我们看下函数源码:

yexk2q.png

关于参数的注释:

1
2
3
4
5
6
7
8
9
10
11
12
Parameters:
- `requests`: A list of write operations (see examples above).
- `ordered` (optional): If ``True`` (the default) requests will be
performed on the server serially, in the order provided. If an error
occurs all remaining operations are aborted. If ``False`` requests
will be performed on the server in arbitrary order, possibly in
parallel, and all operations will be attempted.
- `bypass_document_validation`: (optional) If ``True``, allows the
write to opt-out of document level validation. Default is
``False``.
- `session` (optional): a
:class:`~pymongo.client_session.ClientSession`.

从源码中我们可以看到bulk_write的返回值是BulkWriteResult对象,我们看下BulkWriteResult对象具有哪些属性:

yev2CR.png

推荐阅读:

「Mongo」块操作初体验

mongo批量插入问题(insert_many,bulk_write)

知识就是财富
如果您觉得文章对您有帮助, 欢迎请我喝杯水!