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:

  1. the show action results in url utilizing objectid
  2. displaying 'college.id' in index.html.erb displays objectid

questions:

  1. how use original "id" field parameter
  2. 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

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -