[SERVER-5983] $rename unsets target field x, if multiple renames are specified where at least x has no source Created: 01/Jun/12  Updated: 16/Feb/15  Resolved: 17/Jan/13

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

Type: Bug Priority: Major - P3
Reporter: Philipp Heinze Assignee: Unassigned
Resolution: Done Votes: 0
Labels: commands, update
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux Fedora 64bit, Java Driver and Mongo shell


Issue Links:
Depends
is depended on by DOCS-64 $rename documentation should explain ... Closed
Backwards Compatibility: Major Change
Operating System: ALL
Participants:

 Description   

As explained in DOCS-64 it seems as $rename isn't behaving consistently.

If you're doing $rename :

{"source": "target"}

while the source isn't existent but target is, Target won't be unset. If you're renaming another field which exists, through $rename :

{ "source":"target", "field":"exists"}

target will be unset if source isn't set.

What I would expect is that there's a consistent behavior, favourable $rename shouldn't cause that target fields are overriden if source doesnt exists as $unset is available for deleting fields.

Further Mongoshell tests copy&pasted from the previously mentioned ticket are:

db.c.save({m:{a:1}})
db.c.findAndModify({query:{},update: {$rename: {"m.a":"b.a"}}})
{ "_id" : ObjectId("4fc7737e03938522156ea7fd"), "m" : { "a" : 1 } }
db.c.findAndModify({query:{},update: {$rename: {"m.a":"b.a"}}})
{
"_id" : ObjectId("4fc7737e03938522156ea7fd"),
"b" : { "a" : 1 },
"m" : {
 
}
}
db.c.findAndModify({query:{},update: {$rename: {"m.a":"b.a"}}})
{
"_id" : ObjectId("4fc7737e03938522156ea7fd"),
"b" : { "a" : 1 },
"m" : {
 
}
}
db.c.findAndModify({query:{}, update:{$rename: {"d":"b"}}})
{
"_id" : ObjectId("4fc7737e03938522156ea7fd"),
"b" : { "a" : 1 },
"m" : {
 
}
}
db.c.findAndModify({query:{}, update:{$rename: {"d":"b"}}})
{
"_id" : ObjectId("4fc7737e03938522156ea7fd"),
"b" : { "a" : 1 },
"m" : {
 
}
}
db.c.save({b: {a :1},d: {c:2}})
db.c.findAndModify({query:{},update:{$rename:{"d.c":"b.c","d.a":"b.a"}}})
{
"_id" : ObjectId("4fc774d303938522156ea7fe"),
"b" : { "a" : 1 },
"d" : { "c" : 2 }
}
db.c.find()
{ "_id" : ObjectId("4fc774d303938522156ea7fe"), "b" : { "c" : 2 }, "d" : { } }
 
This does not happen if I do the following:
 
db.c.save({b: {a :1,e:2},d: {c:2}})
db.c.findAndModify({query:{},update:{$rename:{"d.c":"b.c","d.e":"b.e"}}})
{
"_id" : ObjectId("4fc776a903938522156ea803"),
"b" : { "a" : 1, "e" : 2 },
"d" : { "c" : 2 }
}
db.c.find()
{ "_id" : ObjectId("4fc776a903938522156ea803"), "b" : { "a" : 1, "c" : 2 }, "d" : { } }
 
This behavior can be spottet as well with this:
db.c.save({b: {a :1,e:2},d: {c:2, f: 3}})
db.c.findAndModify({query:{},update:{$rename:{"d.c":"b.c","d.e":"b.e","d.a":"b.a"}}})
{
"_id" : ObjectId("4fc7773403938522156ea805"),
"b" : { "a" : 1, "e" : 2 },
"d" : { "c" : 2, "f" : 3 }
}
db.c.find()
{ "_id" : ObjectId("4fc7773403938522156ea805"), "b" : { "c" : 2 }, "d" : { "f" : 3 } }
> db.test.save({a: 1, b: 2, c: 3})
> db.test.find()
{ "_id" : ObjectId("4fc7782f6b4d32f2145678de"), "a" : 1, "b" : 2, "c" : 3 }
 
> db.test.update({}, {$rename: {"a":"b", "nonexistent": "c"}})
> db.test.find()
{ "_id" : ObjectId("4fc7782f6b4d32f2145678de"), "b" : 1 }



 Comments   
Comment by Eliot Horowitz (Inactive) [ 17/Jan/13 ]

Fixed in 2.2 via SERVER-4845

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