[SERVER-26961] $unset by setting to undefined (like JSON.stringify does) Created: 09/Nov/16  Updated: 30/Dec/16  Resolved: 30/Dec/16

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

Type: Improvement Priority: Minor - P4
Reporter: Cyril Auburtin Assignee: David Storch
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

I think it would be interesting if

db.foo.updateOne({i:1}, {$set:{foo:7, bar:undefined}})

does the same than:

db.foo.updateOne({i:1}, {$set:{foo:7}, $unset:{bar:undefined}})

db.foo.updateOne({i:1}, {$set:{foo:7, bar:undefined}}) would be similar to JSON.stringify({foo:7, bar:undefined}) that have only the foo field



 Comments   
Comment by David Storch [ 30/Dec/16 ]

Hi caub,

After further review by the MongoDB Query Team, we have decided that we do not wish to implement this improvement. The canonical way to remove a field using the update subsystem will continue to be the $unset operator. If you wish to have this behavior as syntactic sugar, you can always implement the transformation on the client side as you suggested in your comment above.

Best,
Dave

Comment by Cyril Auburtin [ 09/Nov/16 ]

I think it's hard to do it because other languages don't have an undefined like JS, but I wish it would make its way in nodeJS driver

In JS it's really practical to set to undefined before 'serialization'

// this function can do the job too

function unsetUndefined(updateQuery){
  var o = {};
  for (var k in updateQuery.$set){
    if (updateQuery.$set[k]===undefined) { o[updateQuery.$set[k]]=1; delete updateQuery.$set[k]; }
  }
  return Object.assign(updateQuery, {$unset: Object.assign(updateQuery.$unset||{}, o)});
}

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