How to rename a key name containing dots in a document?

531 views
Skip to first unread message

Shekhar Sahu

unread,
Jul 14, 2016, 4:34:13 AM7/14/16
to mongodb-user
I learned in hard way that "." is an illegal character to be used in a key name. I didn't have any problem when I was inserting documents in the collection. But when I am trying to retrieve, it can't find the key "YR..MODA".

So  I am trying to rename the field.

db.abc.update({}, {$rename:{"YR..MODA":"YRMODA"}}, false, true);

which returns the following error.

The update path 'YR..MODAHRMN' contains an empty field name, which is not allowed.

Is there any turnaround for this? Because, I have a very large size of data approx 22 GB and I would like to try any other option before I re import the data again.
I am using rmongodb package in R.

Regards

Amar

unread,
Jul 22, 2016, 1:05:23 AM7/22/16
to mongodb-user

Hi Shekhar,

Thank you for bringing this to our attention. I have opened two tickets on MongoDB issue tracker based on this.

1. $rename being unable to rename fields with dots: SERVER-25164

An alternative solution to remove the field with the dot and re-add it without the dot using cursor .forEach:

db.test.find().forEach( function(myDoc) {
  myDoc.ab = myDoc["a.b"];
  delete myDoc["a.b"];
  db.test.save(myDoc);
} );

I tested this against the current version (3.2.8) and I suggest that you test it for your specific use case and data sets.

2. Prevent importing field names with dots for mongoimport: TOOLS-1335.

Please keep in mind that rmongodb is a community-supported driver, if it allows fields with dot to be inserted then you may want to raise an issue ticket on rmongodb project issues. Also you may consider using mongolite package which is more actively maintained.

Regards,

Amar


Reply all
Reply to author
Forward
0 new messages