Bulk.find.arrayFilters()

Description

  • Bulk.find.arrayFilters()

New in version 3.6.

Determines which array elements to modify for an update operation onan array field:

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

In the update document, use the $[<identifier>] filteredpositional operator to define an identifier, which you then referencein the array filter documents. You cannot have an array filterdocument for an identifier if the identifier is not included in theupdate document.

Note

The <identifier> must begin with a lowercase letter andcontain only alphanumeric characters.

You can include the same identifier multiple times in the updatedocument; however, for each distinct identifier ($[identifier])in the update document, you must specify exactly onecorresponding array filter document. That is, you cannot specifymultiple array filter documents for the same identifier. Forexample, if the update statement includes the identifier x(possibly multiple times), you cannot specify the following forarrayFilters that includes 2 separate filter documents for x:

  1. // INVALID
  2.  
  3. [
  4. { "x.a": { $gt: 85 } },
  5. { "x.b": { $gt: 80 } }
  6. ]

However, you can specify compound conditions on the same identifierin a single filter document, such as in the following examples:

  1. // Example 1
  2. [
  3. { $or: [{"x.a": {$gt: 85}}, {"x.b": {$gt: 80}}] }
  4. ]
  5. // Example 2
  6. [
  7. { $and: [{"x.a": {$gt: 85}}, {"x.b": {$gt: 80}}] }
  8. ]
  9. // Example 3
  10. [
  11. { "x.a": { $gt: 85 }, "x.b": { $gt: 80 } }
  12. ]

Append to Bulk.find() method to specify the array filtersfor the updateOne() andupdate() operations.

Example

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

See also