MongoDB,批量查找返回什么

时间:2016-03-22 17:51:49

标签: python mongodb bulk

我正在尝试使用批量查找来检索一组文档并返回集合,我想知道批量查找返回什么?我的代码是:

 def bulk_find(collection_name, key, value):

    bulk = db[collection_name].initialize_ordered_bulk_op()

    bulk.find({key: value})

    results = bulk.execute()

那么,bulk.find在这里返回什么?文档没有说明任何内容。

2 个答案:

答案 0 :(得分:5)

返回BulkWriteOperation个实例。来自documentation

find(selector)
Specify selection criteria for bulk operations.

Parameters: 
selector (dict): the selection criteria for update and remove operations.
Returns:    
A BulkWriteOperation instance, used to add update and remove operations to this bulk operation.

pymongo bulk是一个 操作界面。如果要从某个集合中检索多个文档,则应使用相应集合的方法find。你需要

results = db[collection_name].find({key:value})

此操作将返回集合collection_name中的所有文档,其中key字段== value的值。

答案 1 :(得分:2)

bulk.find返回一个接口,允许您对要选择数据的多个操作进行分组。

e.g。更新,删除。

将查询分组并分批发送到MongodB执行非常有效。例如您希望增加某些供应商的产品销售佣金。这个委托不是直截了当的,你必须对它应用一些逻辑。

通常情况下,如果没有bulk.find ...,您将检索所有匹配的文档,进行更改并发送回服务器进行保存。

在bulk.find的情况下,你基本上做同样的事情,但不是将每个文档单独发送回服务器,而是继续批量缓存它们。一旦调用bulk.execute,驱动程序就会将所有更改捆绑在一起 并将它们发送到服务器。

有关详细信息,请参阅https://docs.mongodb.org/manual/reference/method/Bulk.find/