[DOCS-64] $rename documentation should explain the behavior when the original field isn't present Created: 13/Oct/11  Updated: 30/Oct/23  Resolved: 03/Oct/12

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: None
Fix Version/s: Server_Docs_20231030

Type: Improvement Priority: Minor - P4
Reporter: Neil Sanchala Assignee: Kay Kim (Inactive)
Resolution: Done Votes: 0
Labels: update
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-5983 $rename unsets target field x, if mul... Closed
Participants:
Days since reply: 11 years, 18 weeks, 2 days ago

 Description   

The documentation at http://www.mongodb.org/display/DOCS/Updating#Updating-%24rename doesn't specify the behavior of $rename when the source field doesn't exist and the destination field does exist. I believe the current behavior is to unset the destination, which was very surprising to me at least.

Thanks!



 Comments   
Comment by Kay Kim (Inactive) [ 15/Oct/12 ]

Hi Philipp –
the updated documentation on $rename has been published –
http://docs.mongodb.org/manual/reference/operator/rename/

Regards,
Kay

Comment by auto [ 11/Oct/12 ]

Author:

{u'date': u'2012-10-11T14:08:00-07:00', u'email': u'samk@10gen.com', u'name': u'Sam Kleinman'}

Message: merge: DOCS-64
Branch: master
https://github.com/mongodb/docs/commit/ff9a44529234f3b7ba424c93e16de990f6f6b585

Comment by auto [ 11/Oct/12 ]

Author:

{u'date': u'2012-10-10T15:20:01-07:00', u'email': u'kay.kim@10gen.com', u'name': u'kay'}

Message: DOCS-64 minor changes to note
Branch: master
https://github.com/mongodb/docs/commit/90a528a978138b5607ce8b90f8ae265b12e15be1

Comment by auto [ 09/Oct/12 ]

Author:

{u'date': u'2012-10-09T12:54:56-07:00', u'email': u'samk@10gen.com', u'name': u'Sam Kleinman'}

Message: DOCS-64 edits
Branch: master
https://github.com/mongodb/docs/commit/b3c65f17fe8b03e79ef6beaf2c8c47d11a3aaaf8

Comment by auto [ 09/Oct/12 ]

Author:

{u'date': u'2012-10-09T12:47:43-07:00', u'email': u'samk@10gen.com', u'name': u'Sam Kleinman'}

Message: merge: DOCS-64
Branch: master
https://github.com/mongodb/docs/commit/7c33708b0e79fc362fa9e09e35c308bb3da9a424

Comment by auto [ 09/Oct/12 ]

Author:

{u'date': u'2012-10-09T11:56:04-07:00', u'email': u'kay.kim@10gen.com', u'name': u'kay'}

Message: DOCS-64 rename doc minor fixes
Branch: master
https://github.com/mongodb/docs/commit/6419f55b2d276e378c39b5e1992ec41390ba947d

Comment by auto [ 04/Oct/12 ]

Author:

{u'date': u'2012-10-04T13:55:55-07:00', u'email': u'samk@10gen.com', u'name': u'Sam Kleinman'}

Message: DOCS-64: edits
Branch: master
https://github.com/mongodb/docs/commit/171e2eea2b46205b6ba2f613c44b6334b8e9c4d5

Comment by auto [ 04/Oct/12 ]

Author:

{u'date': u'2012-10-04T13:11:18-07:00', u'email': u'samk@10gen.com', u'name': u'Sam Kleinman'}

Message: merge: DOCS-64
Branch: master
https://github.com/mongodb/docs/commit/d4694b5b85aa9fa61362d074d5b0c7b9f7730455

Comment by auto [ 04/Oct/12 ]

Author:

{u'date': u'2012-10-03T14:52:30-07:00', u'email': u'kay.kim@10gen.com', u'name': u'kay'}

Message: DOCS-64 document rename behavior when old name does not exist
Branch: master
https://github.com/mongodb/docs/commit/9a10f8549addca4725862b83ecc7103787d0499e

Comment by Kay Kim (Inactive) [ 04/Oct/12 ]

Hi Philipp – the changes have not yet been merged. Will let you know once it has been merged and published. Regards, Kay

Comment by Philipp Heinze [ 04/Oct/12 ]

After looking into the documentation I can't see any change in regards to rename. Could you please point out where this clarification did go to, or repeat it here? Thank you!

Comment by Philipp Heinze [ 01/Jun/12 ]

I've escalated this ticket into a Bug ticket for SERVER. You can find it here: SERVER-5983

Comment by Neil Sanchala [ 31/May/12 ]

I agree that it feels more like a bug. Eliot didn't when we discussed it, and he suggested opening this docs ticket. It's possible we didn't properly explain the issue there with minimal repro cases.

Comment by Neil Sanchala [ 31/May/12 ]

(I got that shell output running against a 2.0.4 server.)

Comment by Neil Sanchala [ 31/May/12 ]

Sorry, my initial description wasn't sufficient. There have to be multiple renames, where one actually does a rename.

> 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 }
Comment by Philipp Heinze [ 31/May/12 ]

What I've found out is that existent fields are dropped, when all fields of the rename target and rename source subdocument are referenced in $rename. Those fields non existent in the source subdoc will cause the target fields to be unset.

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 }

}

So I may consider this behavior as bug, as it's not consistent, which can cause severe problems if you trust on some common terms of Operation.

Comment by Philipp Heinze [ 31/May/12 ]

With MongoDB 2.0.3 a $rename of a non existent field, doesn't causes that the field, if existent, will be unset. What version did you use?

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" : {

}
}

Generated at Thu Feb 08 07:37:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.