开发者

Mongodb mongoid model attributes sorted by alphabetical order not insertion order

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

  1. 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"} 

  1. 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} 

  1. 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

Field (re)order

During an update the field order may be changed. There is no guarantee that the field order will be consistent, or the same, after an update. At the moment, if the update can be applied in place then the order will be the same (with additions applied at the end), but if a move is required for the document (if the currently allocated space is not sufficient for the update) then the fields will be reordered (alphanumerically).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜