Renaming mongodb Columns
Join the DZone community and get the full member experience.
Join For FreeToday I was putzing around in the geo-spatial collection when I noticed that I had an unhappy over one of the column names within the collection.
In the mySQL world, changing a column name is pretty straight-forward courtesy of the alter table command.
Mongo…not so much…
<BEGIN_UNRELATED_SIDE_RANT>
The Mongo documentation is normally the first place most of us go when we’re looking for help in using our favorite noSQL database.
Why?
Well…because that’s usually where Google directs us to go and also because there just isn’t a whole lot of documentation out there on the subject to begin with.
The mongo (10gen) documentation is pretty good. It’s not, however, excellent. And I can articulate the reason why.
It’s pretty easy to identify documentation written by engineers as opposed to documentation written by everyone else (on the planet). And not because of technical content or the (ab)use of really big and impressive-sounding jargon.
No – it’s because most engineering-authored documents are written using a solution-based voice instead of a problem-based voice.
Think about it: when I have to go to the man-page for help, it’s because I have a problem. If I had a solution, I would be writing a blog post. But since I have a problem, I need the man-pages, online docs, whatever, to help me figure-out a solution.
Engineering documents are written from a solution perspective: the document assumes you possess some bit of arcane lore (which is probably just exactly that little bit of lore that you’re missing which has caused your trip to the documentation vault) and everything that is explained within the document all hinges on this piece of knowledge which the author, albeit with the finest of intentions, assumes is already firmly in your mental possession.
And that’s why I usually don’t like 10gen’s documentation. But, like I said earlier, it’s the only game in (Google)town.
<END_UNRELATED_SIDE_RANT>
In mongo, to change the name of a column within a collection, you first have to be on a release of mongodb 1.7.2 or later. Since most of us bleeding-edge, early-adopter types are all 2.x versioned, this shouldn’t be an issue.
This page from 10Gen is the update page and, within, talked about the $rename modifier to the update command. What the section doesn’t say, because it’s assuming you’re wanting to update records and not schema, is how to apply a change to all of the records in your collection.
In my case, I have a column-name which I fat-fingered the name right out it’s camel-case: CountryID instead of countryID. (And, yes, OCD-peeps, I know that it’s not strictly camelCase, thank-you!) I want to spin through all 3.7 million rows in my collection and rename this column…
> db.geodata_geo.update( {} , { $rename : { 'CountryID' : 'countryID' }}, true, true );
So what we have here is the update command to the collection (geodata_geo) and four parameters:
- {} — the empty set (this is what’s missing from the 10gen doc) implying to do whatever to each record in the collection
- $rename — the modifier to the update command which, in this case: replace ‘CountryID’ with ‘countryID’
- false — indicates to allow upserts if the record does not exist
- true — multi option: means to apply command to all records since, by default, the update() quits after updating the first record
And I run this command and mongo goes off (whirr…whirr … I have two-node replication…) and renames the column in my collection!
What it didn’t do was update my index.
So, after my column-renaming completed, I needed to drop the index(es) that had ‘CountryID’ as members and re-index the collection to reflect the new column name.
Executing getIndexes() confirmed that my mongo world was back in it’s correct orbit and life, once again, was good.
Source: http://shallop.com/2012/01/renaming-mongodb-columns/
Opinions expressed by DZone contributors are their own.
Comments