Title basically describes the question. I’m assuming if it comes out as an array it will be maintained, but if a hash it will not. I’m just not sure which is it using.
Also, I should mention I’m using mongoid (ORM wrapper for Ruby) in case this changes things.
To answer your basic question is no.
In fact, there’s a bug about re-ordering of keys in JIRA. An embedded object is just an object of keys/values stored inside another value. So the ordering is definitely not guaranteed in the DB.
From a BSON perspective, ordering should not matter.
{x:1,y:2}is the same as{y:2,x:1}.From the perspective of the driver, both of those notations should return the same object.
However, if you are using an array of embedded documents, then the array should keeps its order.
So you cannot depend on key ordering for any objects (embedded or not), but the ordering of an array object should work.