Hướng dẫn arrayfilters mongodb

Docs HomeMongoDB Manual

Bulk.find.arrayFilters[]

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

Bulk.find[].arrayFilters[[ , ... ]].updateOne[];
Bulk.find[].arrayFilters[[ , ... ]].update[];

In the update document, use the $[] 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 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:

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

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

Tip

See also:

  • db.collection.initializeUnorderedBulkOp[]

  • db.collection.initializeOrderedBulkOp[]

  • Bulk.find.update[]

  • Bulk.find.updateOne[]

  • Bulk.execute[]

  • All Bulk Methods

Docs HomeMongoDB Manual

➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.

Note

Starting in MongoDB 4.2, MongoDB can accept an aggregation pipeline to specify the modifications to make instead of an update document. See the method reference page for details.

All write operations in MongoDB are atomic on the level of a single document. For more information on MongoDB and atomicity, see Atomicity and Transactions.

Once set, you cannot update the value of the _id field nor can you replace an existing document with a replacement document that has a different _id field value.

For write operations, MongoDB preserves the order of the document fields except for the following cases:

  • The _id field is always the first field in the document.

  • Updates that include renaming of field names may result in the reordering of fields in the document.

With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.

Chủ Đề