开发者

Is the order of embedded objects maintained in MongoDB?

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.


From the Mongoid Docs: 1 to Many embedded relationships are stored as an array of hashes.

http://mongoid.org/docs/relations/embedded/1-n.html

So that means you can use a call like parent.embeddedobject.first to get the first embedded object in the array. You can also use array append notation (person.addresses << Address.new) to add new embedded objects.

I'm also using mongoid, and I found this railscast to be really helpful. Check it out if you haven't already.

http://railscasts.com/episodes/238-mongoid

TL:DR; I'm a noob, but I think the order is preserved.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜