1

In an application I'm making in Ruby, I store objects in a MongoDB database using MongoMapper.

Among other things, I need to store an array that is a property of a document:

{String => { [Strings] }

Or to put it in a more Javascript style notation:

{
    "fooArray" [
        "one",
        "two",
        "three"
    ]
}

Is the order of this array guaranteed to be preserved, or must I do something else to guarantee order? After a few tests it seems to work, but I need to be sure.

BonsaiOak
  • 27,741
  • 7
  • 30
  • 54
MarioDS
  • 12,895
  • 15
  • 65
  • 121

2 Answers2

4

Array is an ordered data structure. So yes, the order should be preserved. Your tests confirm this. If the order wasn't preserved, this would be a major bug in MongoMapper.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
1

Yes, the order of the items inside an array will stay the same.
You can read more here: http://ruby-doc.org/core-2.0/Array.html

Arrays are ordered, integer-indexed collections of any object.

fnkr
  • 9,428
  • 6
  • 54
  • 61