[SERVER-1947] can't push and set in one operation Created: 14/Oct/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: Dwight Merriman Assignee: Backlog - Query Team (Inactive)
Resolution: Won't Do Votes: 24
Labels: positional-operator
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-6566 Support conditional updates: $updates Closed
related to SERVER-1050 not allowed to $push and $pop to same... Closed
Assigned Teams:
Query
Participants:

 Description   

> t.find()
{ "_id" : 3, "version" : 1, "versionsCount" : 1, "name" : "TestName1", "versions" : [

{ "version" : 1, "versionFromInstant" : 33, "versionToInstant" : 99, "name" : "TestName1", "value" : "A" }

] }
> q

{ "_id" : 3, "versions.version" : 1 }

> u
{
"$set" :

{ "version" : 2, "name" : "NEWNAME", "versions.$.versionToInstant" : 1000 }

,
"$inc" :

{ "versionsCount" : 1 }

,
"$push" : {
"versions" :

{ "z" : "anewone" }

}
}
> t.update(q,u)
have conflicting mods in update



 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 done by setting the array to its new value using various aggregation array expressions.

Example:

db.c.update({}, [
      {$set:{array:{$concatArrays:[ {
         $filter:{
             input: "$array",
             as: "a",
             cond: {$gte: [ "$$a", 5 ]}
         },
         [ 10 ] 
      }]}}}
]);

There are several ways to do this in aggregation.

Comment by Christopher Price [ 15/Jul/15 ]

Would love to be able to do something like this:

use tmp;
db.tmp.insert({'_id':'A', '_array' : [1,2,3,4,5,6,7,6,5,4,3,2,1]});
db.tmp.update({'_id':'A'}, {'$pull' : {'_array' : {'$lt' : 5}}, '$addToSet' : {'_array' : 10}});

We currently have to do this in two statements.

Comment by Dwight Merriman [ 17/Sep/12 ]

slightly simpler example:

> t.find()
{ "_id" : 3, "name" : "NEWNAME", "version" : 2, "versions" : [

{ "name" : "TestName1", "value" : "A", "version" : 1, "versionFromInstant" : 33, "versionToInstant" : 1000 }

, 999 ], "versionsCount" : 1 }
> t.update({},z)
have conflicting mods in update
> z
{
"$set" :

{ "versions.0.versionToInstant" : 1000 }

,
"$push" :

{ "versions" : 999 }

}

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