Two "id" fields in one MongoDB collection with Rails 3? -
i've got rails 3.0.9 project using latest version of mongodb , mongoid 2.2.
i imported csv "id" field mongodb collection named college, resulting in collection so:
{ "_id" : objectid("abc123"), "id" : ######, ... }
observations:
- the show action results in url utilizing objectid
- displaying 'college.id' in index.html.erb displays objectid
questions:
- how use original "id" field parameter
- is "id" reserved mongodb, meaning need rename "id" field in college collection (perhaps "code") - if so, how?
thanks!
update
answer:
db.colleges.update( { "name" : { $exists : true } } , { $rename : { "id" : "code" } }, false, true )
i used "name" since field check existence.
_id reserved , required property in mongodb - think mongoid mapping id _id since makes sense. there might way access id property through mongoid think better off renaming id column else avoid confusion in future.
{ $rename : { old_field_name : new_field_name } }
will rename field name in document (mongo 1.7.2+).
so
db.college.update({ "_id" : { $exists : true }}, { $rename : { 'id' : 'code' } }, false, true);
should update every record in collection , rename id field code.
(obviously test before running in important data)
Comments
Post a Comment