[SERVER-33482] update existing item or push a new item to an array in a document Created: 26/Feb/18  Updated: 08/Jun/21  Resolved: 26/Feb/18

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

Type: New Feature Priority: Major - P3
Reporter: Dinesh Chander Assignee: Ramon Fernandez Marina
Resolution: Duplicate Votes: 0
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
Participants:

 Description   

I want to update an object or push an object to an array in one query? For e.g if the document is

{list: [{a: 1}]}

I want to add a new object

{b: 2}

to list if this object doesn't exists in the list otherwise I want to update it.

db.temp.update({
  list: {
    $exists: true
  }
},
{
  $set: { 
    "list.$[elem].a1": 10, 
  },
  $pushIfNoMatch: {
    list: {b: 2}
  }
}, 
{
  arrayFilters: [{
    "elem.a": {
      $ne: 1 
    }
  }], 
  
  multi: true,
  upsert: true
})

Since condition in arrayFilter will return false, $pushIfNoMatch operator will work and the new state of document will be

{list: [{a: 1}, {b: 2}]}

if we change the condition to $eq: 1, the $set operator will work and new state will be

{list: [{a: 1, a1: 10}]}



 Comments   
Comment by Ewan Higgs [ 07/Jun/21 ]

SERVER-6566 was closed as WON'T DO.

I think this is a valuable feature and much simpler than the alternatives proposed in SERVER-6566 or the alternative of downloading a potentially large document, doing updates, and resolving the differences locally while holding a transaction lock, and then pushing the document again.

Basically, arrays support insert ($push), update (using arrayFilters), and this ticket is about making upsert operations on arrays.

Can this be reopened? Or would I need to make a new ticket?

 

Thanks

Comment by Ramon Fernandez Marina [ 26/Feb/18 ]

dispareil, I think the functionality you're requesting is a subset of SERVER-6566, so I'm going to close this ticket as a duplicate. Please feel free to vote for and watch SERVER-6566.

Thanks,
Ramón.

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