[SERVER-27089] Extend the update subsystem to support more expressive updates to array fields Created: 17/Nov/16  Updated: 28/Apr/18  Resolved: 11/Aug/17

Status: Closed
Project: Core Server
Component/s: Querying, Write Ops
Affects Version/s: None
Fix Version/s: 3.5.12

Type: New Feature Priority: Major - P3
Reporter: David Storch Assignee: Tess Avitabile (Inactive)
Resolution: Done Votes: 33
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PDF File Design Array updates.pdf    
Issue Links:
Depends
is depended on by SERVER-6982 positional operator does not work wit... Closed
is depended on by SERVER-831 Positional Operator Matching Nested A... Closed
is depended on by SERVER-1243 New operator to update all matching i... Closed
is depended on by SERVER-2476 New $update atomic modifier for chang... Closed
is depended on by SERVER-25717 Allow for negative $push indexes. Closed
is depended on by SERVER-1050 not allowed to $push and $pop to same... Closed
Duplicate
is duplicated by SERVER-6982 positional operator does not work wit... Closed
is duplicated by SERVER-29526 Updating the specific array in nested... Closed
Related
related to SERVER-30998 Update and $ operator mismatch with m... Closed
is related to SERVER-25717 Allow for negative $push indexes. Closed
Backwards Compatibility: Fully Compatible
Sprint: Query 2017-08-21
Participants:
Case:

 Description   
Issue Status as of Aug 11, 2017

FEATURE DESCRIPTION
MongoDB 3.5.12 extends all update modifiers to apply to all array elements or all array elements that match a predicate, specified in a new update option arrayFilters. This syntax also supports nested array elements.

VERSIONS
This new feature is available starting with the MongoDB 3.5.12 development version, and included in the MongoDB 3.6 production version.

OPERATION

Update all documents in array

db.coll.update({}, {$set: {“a.$[].b”: 2}})
Input: {a: [{b: 0}, {b: 1}]}
Output: {a: [{b: 2}, {b: 2}]}

Update all matching documents in array

db.coll.update({}, {$set: {“a.$[i].b”: 2}}, {arrayFilters: [{“i.b”: 0}]})
Input: {a: [{b: 0}, {b: 1}]}
Output: {a: [{b: 2}, {b: 1}]}

Update all matching scalars in array

db.coll.update({}, {$set: {“a.$[i]”: 2}}, {arrayFilters: [{i: 0}]})
Input: {a: [0, 1]}
Output: {a: [2, 1]}

Update all matching documents in nested array

db.coll.update({}, {$set: {“a.$[i].c.$[j].d”: 2}}, {arrayFilters: [{“i.b”: 0}, {“j.d”: 0}]})
Input: {a: [{b: 0, c: [{d: 0}, {d: 1}]}, {b: 1, c: [{d: 0}, {d: 1}]}]}
Output: {a: [{b: 0, c: [{d: 2}, {d: 1}]}, {b: 1, c: [{d: 0}, {d: 1}]}]}

Update all scalars in array matching a logical predicate

db.coll.update({}, {$set: {“a.$[i]”: 2}}, {arrayFilters: [{$or: [{i: 0}, {i: 3}]}]})
Input: {a: [0, 1, 3]}
Output: {a: [2, 1, 2]}

Each array filter must be a predicate over a document with a single field name. Each array filter must be used in the update expression, and each array filter identifier $[<id>] must have a corresponding array filter. <id> must begin with a lowercase letter and not contain any special characters. There must not be two array filters with the same field name.

IMPLEMENTATION DETAILS
The implementation of this feature involved a rewrite of the update system. Users can find all the related tickets here. The design document is attached.

Original description


 Comments   
Comment by Tess Avitabile (Inactive) [ 11/Aug/17 ]

Resolving, as all implementation is complete in 3.5.12.

Comment by David Storch [ 20/Dec/16 ]

asya, agreed. This ticket is intended to capture all improvements related to updating array fields that we believe to be important.

Comment by Asya Kamsky [ 20/Dec/16 ]

Suggestion to add to the list of things this project/ticket will address "Additional array manipulations" - to take care of things like $push to negative position (SERVER-25717) and others.

Generated at Thu Feb 08 04:14:08 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.