0

Following up on this 1 year old post:

Mongodb mongoid model attributes sorted by alphabetical order not insertion order

I have a rails 3.2.8 application that uses mongoid v. 3.0.0. My application occasionally uses the "update_attribute" function on a database object (e.g. a product), but when it does all attributes gets reordered alphabetically.

When I duplicate this in the console, it looks like this:

1.9.3-p385 :001 > product = Product.find("5156bc9b83c3368121000008")
 => [#<Product _id: 5156bc9b83c3368121000008, _type: nil, product_number: "123", product_name: "Some product name", long_description: "<P>Some long product description.</P>", vendor_number: "abc", language_i_d: "1234", currency_i_d: "USD", category_number: "1", image_link: "http://some-external-website.com/image-path.jpg", original_id: "123456">]
1.9.3-p385 :002 > product.update_attribute(:image_link, "http://my-own-website.com/image-path.jpg")
 => true
1.9.3-p385 :003 > exit

I now fire up the console again (for some reason I need to exit and re-open the console before the new order is displayed):

1.9.3-p385 :001 > product = Product.find("5156bc9b83c3368121000008")
 => [#<Product _id: 5156bc9b83c3368121000008, _type: nil, category_number: "1", currency_i_d: "USD", image_link: "http://my-own-website.com/image-path.jpg", language_i_d: "1234", long_description: "<P>Some long product description.</P>", original_id: "123456", product_name: "Some product name", product_number: "123", vendor_number: "abc">] 

Does anyone know how to avoid this reordering?

4

1 回答 1

0

This isn't mongoid's fault. If an update causes a document to grow and mongo had to move the document as a result then mongodb itself may reorder the document's fields (see docs and a jira issue where this is described as normal)

于 2013-03-31T00:17:06.090 回答