[SERVER-786] Update a size field with addToSet Created: 18/Mar/10  Updated: 06/Dec/22  Resolved: 29/Jun/19

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

Type: Improvement Priority: Minor - P4
Reporter: Marc Seeger Assignee: Backlog - Query Team (Inactive)
Resolution: Done Votes: 20
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-6566 Support conditional updates: $updates Closed
Assigned Teams:
Query
Participants:

 Description   

As mentioned over here: http://groups.google.com/group/mongodb-user/browse_thread/thread/9c4e2a157cc8e0c9

It would be nice to have the ability to update a size field when doing a pushToSet (especially in connection with more than one element and $each). One can't simply use $inc since there is no information how many elements were actually pushed



 Comments   
Comment by Asya Kamsky [ 29/Jun/19 ]

SERVER-40381 implemented support for aggregation expressions to specify update for 4.2. 

You can see some examples here.

This can be handled by setting a size field to {$size:<array>} after the $addToSet (which would be $setUnion in aggregation syntax.

db.c.update({}, [
      {$set:{a:{$setUnion:[ "$a", [ <newElement>] ]}}},
      {$set:{sizeA:{$size:"$a"}}}
]);

Comment by Asya Kamsky [ 16/May/17 ]

Related to conditional updates

Comment by Piyush Verma [ 02/Nov/12 ]

+1 https://jira.mongodb.org/browse/SERVER-4376 is Similar.

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