mongoid/rails - renaming data fields

1,757 views
Skip to first unread message

sergio_101

unread,
Mar 22, 2012, 12:28:23 AM3/22/12
to Mongoid
hey, all..

please bear with me, as i am working on my first project with mongoid
and rails. this is the this is my first time using a document based
database, so i am trying to make sure i am thinking about this
correctly.

in the past, if i has a model with the fields

firstname
lastname

and they were filled with data, i would have a table called 'users'
with those fields.

supposed i wanted to change the names of the fields to something like:

first_name
last_name

i would write a migration to change the names of the fields to the
above. in doing so, i would simply change the names of the fields, and
all data would be retained..

suppose i have the same scenario using mongoid/mongodb.

my first inclination would be to change the fields name in the model.
doing so would change the names of the fields when new data is stored.
at this point, what happens to the old data? is there some sort of
conversion of the fields names to the old documents? do i need to
write a migration to change the field names of the old data?

in other words, am i left with all my old records with fields:

firstname
lastname

and all new records using:

first_name
last_name

or is there a conversion somewhere?

sorry if this is a silly question, and i supposed i could flesh out
the answers experimentally, but in asking, i am hoping to learn a bit
about the philosophy of document based databases.

thanks!

Kristina Clair

unread,
Mar 22, 2012, 10:37:08 AM3/22/12
to mon...@googlegroups.com
Since MongoDB is schema-less, if you rename the fields in your model, all of your old records will simply retain their records with the old field names.

If you wanted to convert them, I believe you would need to write a custom migration.  This wouldn't be like an ActiveRecord migration where you can re-name columns by operating on the table directly.  Instead, in your def up method, you would need to loop through all of your records and create fields with the new field name on them (and delete the old field name if you like). 

At least, that is how I understand it.

sergio_101

unread,
Mar 22, 2012, 10:47:57 AM3/22/12
to Mongoid
> re-name columns by operating on the table directly.  Instead, in your def
> up method, you would need to loop through all of your records and create
> fields with the new field name on them (and delete the old field name if

this is exactly what i thought would have to happen.. which is fine,
as renaming columns doesn't happen all that often.. and if it did,
there would still be some cleanup in attribute names scattered through
the models..

honestly, i only thought about it because during a development
iteration (but before any code was committed), i realized that i had
made a typo in my attribute name. i changed the name in the model, and
went on my merry way. at that point, i started to wonder how to handle
such a case.

a 'data cleanup' migration totally makes sense.

thanks!

rubish gupta

unread,
Mar 22, 2012, 12:39:32 PM3/22/12
to mon...@googlegroups.com
When writing a migration I generally tend to use operations at a collection level rather than for each document if I can.

For example recently I needed to change the field :activated_at to :invitation_accepted_at

User.collection.update({}, 
    {'$rename' => {'activated_at' => 'invitation_accepted_at'}},
    multi: true, safe: true)

sergio_101

unread,
Mar 22, 2012, 12:48:36 PM3/22/12
to Mongoid
> For example recently I needed to change the field :activated_at to
> :invitation_accepted_at
>
> User.collection.update({},
>     {'$rename' => {'activated_at' => 'invitation_accepted_at'}},
>     multi: true, safe: true)


perfect! actually, i was just looking at all the dollar-operations...
to see what was available..

thanks!
Reply all
Reply to author
Forward
0 new messages