[SERVER-6124] $unset may create a nested field if it doesn't exist Created: 19/Jun/12  Updated: 11/Jul/16  Resolved: 09/Dec/13

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: 2.0.3, 2.1.2
Fix Version/s: 2.5.5

Type: Bug Priority: Minor - P4
Reporter: Olga Ignatova Assignee: Scott Hernandez (Inactive)
Resolution: Done Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-6047 $pullAll causes empty embedded object... Closed
Operating System: ALL
Participants:

 Description   

It seems that in some situations an update with $unset operator may create nested fields. It only seems to happen if at least one of the fields of the $unset operator matches an existing field in the document.

E.g.

> db.test.save({a:1})
> db.test.find()
{"_id" : ObjectId("..."), "a" : 1}
> db.test.update({}, {$unset:{"a":1, "b.c":1}}, false, true)
> db.test.find()
{"_id" : ObjectId("..."), "b" : {}}

The field "b.c" didn't exist in the original document and an empty object "b" got created as a result of the update.
Note, that this only happens when at least one of the fields used by the $unset operator exists in the document. If I modify the update() operation as follows, no insertion happens:

> db.test.update({}, {$unset:{"b.c":1}}, false, true)
> db.test.find()
{"_id" : ObjectId("..."), "a" : 1}



 Comments   
Comment by Githook User [ 09/Dec/13 ]

Author:

{u'username': u'scotthernandez', u'name': u'Scott Hernandez', u'email': u'scotthernandez@gmail.com'}

Message: SERVER-6124: $unset may create a nested field if it doesn't exist
Branch: master
https://github.com/mongodb/mongo/commit/32311b5f0e7a6d00cc2253d7f21397b51f0e126b

Comment by Scott Hernandez (Inactive) [ 20/Nov/13 ]

No longer happens with new update framework.

> db.tunset.save({_id:1, a:1})
Update WriteResult({ "ok" : 1, "n" : 1, "upserted" : 1 })
test1:PRIMARY> db.tunset.update({}, {$unset:{"a":1, "b.c":1}})
Update WriteResult({ "ok" : 1, "n" : 1 })
test1:PRIMARY> db.tunset.find()
{ "_id" : 1 }

Need to add a test for regressions before closing.

Comment by Ben Becker [ 19/Jun/12 ]

Confirmed this behavior in master.

Generated at Thu Feb 08 03:10:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.