[SERVER-25164] $rename doesn't rename fields with dots (.) Created: 20/Jul/16  Updated: 03/Oct/16  Resolved: 03/Oct/16

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: 3.2.7
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Amar Hamzeh Assignee: Kelsey Schubert
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-15029 $rename modifier uses incorrect dotte... Closed
related to SERVER-20171 Invalid "cannot use the part to trave... Closed
related to TOOLS-1335 mongoimport imports fields with dots ... Closed
Operating System: ALL
Participants:

 Description   

`$rename` cannot rename fields with dots (.) in their names:

A collections like:

> db.test.find()
{ "_id" : ObjectId("578ec46e39b3c19fbbeb2264"), "a.b" : 1 }
{ "_id" : ObjectId("578ec46e39b3c19fbbeb2262"), "a.b" : 2 }
>

won't get updated with:

> db.test.update({}, { $rename : { "a.b" : "ab" }}, false, true);
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 0 })
>
> db.test.find()
{ "_id" : ObjectId("578ec46e39b3c19fbbeb2264"), "a.b" : 1 }
{ "_id" : ObjectId("578ec46e39b3c19fbbeb2262"), "a.b" : 2 }
>



 Comments   
Comment by Kevin Pulo [ 20/Jul/16 ]

This is precisely why fields containing dots are not considered valid for storage, and the storage validation checks have to be overridden/ignored in order to get the documents in there in the first place (or they are legacy docs).

When you say:

db.test.update({}, { $rename : { "a.b" : "ab" }}, false, true)
                                 ^^^^^

or even just:

db.test.find( { "a.b" : "foobar" } )
               ^^^^^

is the "a.b" supposed to refer to a field like this:

{ _id: ..., "a.b": "foobar" }
            ^^^^^^^^^^^^^^^

or to a field like this:

{ _id: ..., a: { b: "foobar" } }
                 ^^^^^^^^^^^

In the presence of dots in field names, this is always hopelessly ambiguous. You can't even query properly for fields like that. This is why dots are not allowed in field names, and the server chooses to interpret "a.b" in the latter style.

This means that the $rename operator will be looking for a field "a", not finding it, and so correctly performing a no-op. (eg. see SERVER-20171 and SERVER-15029.)

Fixing any documents that do happen to contain dotted fields like this will likely involve manually scanning and adjusting them (probably client-side).

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