Navigation
This is an upcoming (i.e. in progress) version of the manual.

Bulk.find.arrayFilters()

On this page

Description

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

New in version 3.6.

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. 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.

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. 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:

[
  { "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:

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

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

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

Append to Bulk.find() method to specify the array filters for the updateOne() and update() operations.

Example

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