0

I have a document:

{
    _id : 123412341234

    regions : {
        america : [north, south, central]
        europe : [east, west]
        asia : [north, south, east, west, central]
    }

}

I want to return the 2nd object (europe) in the regions object of the document by specifying its index.

Example:

db.area.find({}, {regions.2 : 1})

Return:

[east, west]

How do I return the 2nd object (europe) of my regions object?

samland
  • 192
  • 1
  • 12
  • You can't get that value using indexing. Do you want the value where `key==='europe'`? – styvane Jan 15 '16 at 19:46
  • Yes. I want the value key==='europe' without specifying that it is regions.europe. I want the 2nd object from regions whatever that might be. – samland Jan 15 '16 at 20:40
  • 5
    You can't use indexing because JavaScript doesn't order object property you may want to read this thread.[Does JavaScript Guarantee Object Property Order?](http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) To do want you want you will need to change your data structure and change `regions` to an array of sub-documents. – styvane Jan 15 '16 at 20:46

0 Answers0