Mongoid Keys, Convert BSON::ObjectId to string, string to BSON::ObjectId


1. Convert BSON::ObjectId to string

$profile = Profile.where(“profiles_manageds.profile_fname”=>’Admin’).first
=> #<Profile _id: 4fe969dd79216d0af9000002, _type: nil, user_id: nil, organization_id: nil, email: nil, fname: “Ankit”, lname: “Mishra”, mname: “Kumar”, nickname: nil, gender: “Male”, birthdate: “15/05/1988”, firstRegistrationDate: nil, lastModifiedBy: nil, lastModifiedDate: “15/01/2011”, notes: nil, registeredById: nil, status: “active”, org_id: nil, measurements: nil, immunizations: nil, med_tests: nil, organizationsManaged: nil, parent_ids: nil, physicians: nil, profiles_manageds: [{“_id”=>BSON::ObjectId(‘4fe969dd79216d0af9000001’), “profile_id”=>”4fe9569979216d14ff000001”, “profile_fname”=>”Admin”, “profile_mname”=>”Kids”, “profile_lname”=>”link”}], registrationPayments: nil, sibling_ids: nil, organizationsAdministrated: nil, contacts: nil, default_emergencyContacts: nil, default_pickups: nil, documents: nil, child_extra_cares: nil, enrollments: nil, m_email: “”, m_fname: nil, m_lname: nil, m_phone_home: nil, m_phone_work: nil, m_phone_mobile: nil>
1.9.3-p125 :077 >
1.9.3-p125 :078 >
1.9.3-p125 :079 >   profile
=> #<Profile _id: 4fe969dd79216d0af9000002, _type: nil, user_id: nil, organization_id: nil, email: nil, fname: “Ankit”, lname: “Mishra”, mname: “Kumar”, nickname: nil, gender: “Male”, birthdate: “15/05/1988”, firstRegistrationDate: nil, lastModifiedBy: nil, lastModifiedDate: “15/01/2011”, notes: nil, registeredById: nil, status: “active”, org_id: nil, measurements: nil, immunizations: nil, med_tests: nil, organizationsManaged: nil, parent_ids: nil, physicians: nil, profiles_manageds: [{“_id”=>BSON::ObjectId(‘4fe969dd79216d0af9000001’), “profile_id”=>”4fe9569979216d14ff000001”, “profile_fname”=>”Admin”, “profile_mname”=>”Kids”, “profile_lname”=>”link”}], registrationPayments: nil, sibling_ids: nil, organizationsAdministrated: nil, contacts: nil, default_emergencyContacts: nil, default_pickups: nil, documents: nil, child_extra_cares: nil, enrollments: nil, m_email: “”, m_fname: nil, m_lname: nil, m_phone_home: nil, m_phone_work: nil, m_phone_mobile: nil>
1.9.3-p125 :080 > profile.id
=> BSON::ObjectId(‘4fe969dd79216d0af9000002’)
1.9.3-p125 :081 >
1.9.3-p125 :082 >
1.9.3-p125 :083 >   profile.id.to_s
=> “4fe969dd79216d0af9000002”

2. Convert string to BSON::ObjectId

$ profile.id.to_s
=> “4fe969dd79216d0af9000002”
1.9.3-p125 :093 > profile_id = profile.id.to_s
=> “4fe969dd79216d0af9000002”
1.9.3-p125 :094 >
1.9.3-p125 :095 >
1.9.3-p125 :096 >   profile_id
=> “4fe969dd79216d0af9000002”
1.9.3-p125 :097 >
1.9.3-p125 :098 >
1.9.3-p125 :099 >   BSON::ObjectId.from_string(profile_id)
=> BSON::ObjectId(‘4fe969dd79216d0af9000002’)

Advertisements

2 thoughts on “Mongoid Keys, Convert BSON::ObjectId to string, string to BSON::ObjectId

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