Take the 2-minute tour ×

I have document User and I want to store ordered stack of another users in this document. Example structure of User document:

  "_id": ObjectId("parentID"),
  "stack": [
    { "_id": ObjectID("ChildId2"), position: 1 },
    { "_id": ObjectID("ChildId3"), position: 2 },
    { "_id": ObjectID("ChildId1"), position: 3 }

How can I do this with Mongoid? Generally I want to get ordered (by adding or by field) embeds_many association.

share|improve this question

1 Answer 1

Not sure exactly what you need here but you can specify the order of embedded documents using the order option on the association. For example:

embeds_many :stack, :order => :position.asc

will ensure that user.stack returns the objects on the stack ordered by position.

share|improve this answer

Your Answer


