MongoDB: Changing column names and column types

Changing column name:  The following command will change the field called name to nickname in the collection person

db.person.update({}, {$rename: {"name" : "nickname"}}, false, true)

Changing column type: The following command will change the field called age from type String to Integer.

db.person.find().forEach( function (x) { 
x.age = parseInt(x.age);
db.person.save(x);
});

 

Advertisements

One thought on “MongoDB: Changing column names and column types

  1. Hi there I am so grateful I found your web site,
    I really found you by error, while I was
    searching on Yahoo for something else, Anyways I am
    here now and would just like to say thanks for a tremendous
    post and a all round entertaining blog (I also love the theme/design), I don’t have time
    to read it all at the minute but I have bookmarked it and also included your RSS feeds, so when I have
    time I will be back to read more, Please do keep up the awesome work.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s