I have a model User where I make heavy use of dynamic attributes.
When I am displaying the user show, I skip the first set of attributes to skip the id etc.
Problem is that the attributes are sorted by alphabetical order, not by order of creation
so if for example I create a users as:
MONGODB startuplab_co_development['users'].insert([{"provider"=>"google", "uid"=>"https://www.google.com/accounts/o8/id?id=AItOawl_oas_", "_id"=>BSON::ObjectId('4dc5ad606acb26049e000002'), "email"=>"dan@gmail.com", "first_name"=>"Daniel", "last_name"=>"Palacio", "name"=>"Daniel Palacio"}])
Even though the insertion second attribute is uid, when I retrieve the keys they are sorted alphabetically.
%h1 User
%ul
-keys = @user.attributes.keys[3..-1]
- keys.each do |key|
%li
%span
%strong= "#{key.capitalize()}:"
%span= "#{@user[key]}"
So for instance this will print UID as the last attribute, since they were sorted.
First_name: Daniel
Last_name: Palacio
Name: Daniel Palacio
Provider: google
Uid: https://www.google.com/accounts/o8/id?id=AItOawl_oas_8jcY1VSTQchsc
Is there anyway that I can make sure the attributes position stay in the order of insertion ?
Here is the chain of events where attributes get sorted
- After creation uid is the 3rd attribute
ruby-1.9.2-p0 > user = User.first
=> _id: 4dc5c5946acb26049e000005, _type: nil, _id: BSON::ObjectId('4dc5c5946acb26049e000005'), provider: "google", uid: "https://www.google.com/accounts/o8/id?id=AItOawl_oas_", admin: nil, email: "danpal@gmail.com", first_name: "Daniel", last_name: "Palacio", name: "Daniel Palacio">
ruby-1.9.2-p0 > user.attributes
=> {"_id"=>BSON::ObjectId('4dc5c5946acb26049e000005'), "provider"=>"google", "uid"=>"https://www.google.com/accounts/o8/id?id=AItOawl_oas_", "email"=>"danpal@gmail.com", "first_name"=>"Daniel", "last_name"=>"Palacio", "name"=>"Daniel Palacio"}
- Now we update the admin attribute and save it, uid is still the 3rd attribute
ruby-1.9.2-p0 > user.update_attributes(:admin => true)
=> true
ruby-1.9.2-p0 > user.attributes
=> {"_id"=>BSON::ObjectId('4dc5c5946acb26049e000005'), "provider"=>"google", "uid"=>"https://www.google.com/accounts/o8/id?id=AItOawl_oas_", "email"=>"danpal@gmail.com", "first_name"=>"Daniel", "last_name"=>"Palacio", "name"=>"Daniel Palacio", "admin"=>true}
ruby-1.9.2-p0 > user.save
=> true
ruby-1.9.2-p0 > user.attributes
=> {"_id"=>BSON::ObjectId('4dc5c5946acb26049e000005'), "provider"=>"google", "uid"=>"https://www.google.com/accounts/o8/id?id=AItOawl_oas_", "email"=>"danpal@gmail.com", "first_name"=>"Daniel", "last_name"=>"Palacio", "name"=>"Daniel Palacio", "admin"=>true}
- Know we retrieve the object from the database again, uid is now the last attribute, and they have been sorted alphabetically.
ruby-1.9.2-p0 > user = User.first
=> #
ruby-1.9.2-p0 > user.attributes
=> {"_id"=>BSON::ObjectId('4dc5c5946acb26049e000005'), "admin"=>true, "email"=>"danpal@gmail.com", "first_name"=>"Daniel", "last_name"=>"Palacio", "name"=>"Daniel Palacio", "provider"=>"google", "uid"=>"https://www.google.com/accounts/o8/id?id=AItOawl_oas_"}
Ok here is the answer:
Basically during an update if the Document allocated space is not sufficient( Eg. becouse the update add’s a new field or grows an existing field), the document will be moved and the fields are reordered(alphanumerically).
From the MOngoDB docs:
http://www.mongodb.org/display/DOCS/Updating