I’ve got a Rails 3.0.9 project using the latest version of MongoDB and Mongoid 2.2.
I imported a CSV with an “id” field into a MongoDB collection named College, resulting in a collection like so:
{ "_id" : ObjectID("abc123"), "id" : ######, ... }
Observations:
- The show action results in a URL utilizing the ObjectID
- Displaying ‘college.id’ in index.html.erb displays the ObjectID
Questions:
- How do I use the original “id” field as the parameter
- Is “id” reserved by MongoDB, meaning I need to rename the “id” field in the
College collection (perhaps to “code”) – if so, how?
Thanks!
Update
Answer:
db.colleges.update( { "name" : { $exists : true } } , { $rename : { "id" : "code" } }, false, true )
I used “name” since that was a field I could check for the existence.
_id is a reserved and required property in MongoDB – I think mongoid is mapping id to _id since that makes sense. There might be a way to access the id property through mongoid but I think you are much better off renaming the id column to something else to avoid confusion in the future.
will rename the field name in a document (mongo 1.7.2+).
so
should update every record in that collection and rename the id field to code.
(obviously test this before running in any important data)