Bulk.find.arrayFilters()

On this page本页内容

Description描述

Bulk.find.arrayFilters(<array of filter documents>)

Determines which array elements to modify for an update operation on an array field:确定要为数组字段的更新操作修改哪些数组元素:

Bulk.find(<query>).arrayFilters([ <filter1>, ... ]).updateOne(<update>);
Bulk.find(<query>).arrayFilters([ <filter1>, ... ]).update(<update>);

In the update document, use the $[<identifier>] filtered positional operator to define an identifier, which you then reference in the array filter documents. 在更新文档中,使用$[<identifier>]筛选位置运算符定义标识符,然后在数组筛选器文档中引用该标识符。You cannot have an array filter document for an identifier if the identifier is not included in the update document.如果更新文档中未包含标识符,则不能为该标识符创建数组筛选器文档。

Note注意

The <identifier> must begin with a lowercase letter and contain only alphanumeric characters.<identifier>必须以小写字母开头,并且仅包含字母数字字符。

You can include the same identifier multiple times in the update document; however, for each distinct identifier ($[identifier]) in the update document, you must specify exactly one corresponding array filter document. 您可以在更新文档中多次包含同一标识符;但是,对于更新文档中的每个不同标识符($[identifier]),您必须指定一个对应的数组筛选器文档。That is, you cannot specify multiple array filter documents for the same identifier. 也就是说,不能为同一标识符指定多个数组筛选器文档。For example, if the update statement includes the identifier x(possibly multiple times), you cannot specify the following for arrayFilters that includes 2 separate filter documents for x:例如,如果update语句包含标识符x(可能多次),则不能为包含x的两个独立筛选器文档的arrayFilters指定以下内容:

// INVALID
[
  { "x.a": { $gt: 85 } },
  { "x.b": { $gt: 80 } }
]

However, you can specify compound conditions on the same identifier in a single filter document, such as in the following examples:但是,您可以在单个筛选器文档中的同一标识符上指定复合条件,例如在以下示例中:

// Example 1
[
  { $or: [{"x.a": {$gt: 85}}, {"x.b": {$gt: 80}}] }
]
// Example 2
[
  { $and: [{"x.a": {$gt: 85}}, {"x.b": {$gt: 80}}] }
]
// Example 3
[
  { "x.a": { $gt: 85 }, "x.b": { $gt: 80 } }
]

Append to Bulk.find() method to specify the array filters for the updateOne() and update() operations.附加到Bulk.find()方法以指定updateOne()update()操作的数组筛选器。

Example示例

var bulk = db.coll.initializeUnorderedBulkOp();
bulk.find({}).arrayFilters( [ { "elem.grade": { $gt: 85 } } ] ).updateOne( { $set: { "grades.$[elem].mean" : 70 } } );
bulk.execute();
←  Bulk.find()Bulk.find.collation() →