I must return the documents within an order sorted through which supports the cheapest foo.bar value (that are array objects).

I'm able to do db.collection.find().sort({foo.0.bar: 1}), but this only matches the very first aspect in the array - and as you can tell within the exampe below would sort item 1 first (foo..bar = 5), wheras I'm searching to come back item 2 first (foo.2.bar = 4) because it has got the object using the cheapest value.

{
    "name": "Item 1"
    "foo": [
        {
            "bar": 5,
        }
        {
            "bar": 6,
        }
        {
            "bar": 7,
        }
    ]
}
{
    "name": "item 2"
    "foo": [
        {
            "bar": 6,
        }
        {
            "bar": 5,
        }
        {
            "bar": 4,
        }
    ]
}

Thanks

An alternative choice to using map/reduce, would be to keep min value in the array out like a separate area within the document for you to then order on. Whenever you increase the array, additionally you update this area when the new value is gloomier compared to presently recorded minimum.

e.g. the first document would become this (note "minbar added"):

{
    "name": "Item 1",
    "minbar" : 5,
    "foo": [
        {
            "bar": 5,
        }
        {
            "bar": 6,
        }
        {
            "bar": 7,
        }
    ]
}

There's no direct method of doing this in mongo. You should use map/reduce to retrieve the min val in every array after which sort with that min value

It appears mongo can do that.

For instance, basically possess the following documents:

{ a:{ b:[ {c:1}, {c:5 } ] } }
{ a:{ b:[ {c:0}, {c:12} ] } }
{ a:{ b:[ {c:4}, {c:3 } ] } }
{ a:{ b:[ {c:1}, {c:9 } ] } }

And run the next:

db.collection.find({}).sort({ "a.b.c":1 });
// produces:
{ a:{ b:[ {c:0}, {c:12} ] } }
{ a:{ b:[ {c:1}, {c:5 } ] } }
{ a:{ b:[ {c:1}, {c:9 } ] } }
{ a:{ b:[ {c:4}, {c:3 } ] } }

db.collection.find({}).sort({ "a.b.c":-1 });
// produces:
{ a:{ b:[ {c:0}, {c:12} ] } }
{ a:{ b:[ {c:1}, {c:9 } ] } }
{ a:{ b:[ {c:1}, {c:5 } ] } }
{ a:{ b:[ {c:4}, {c:3 } ] } }

As you can tell, the type by {"a.b.c":1} takes the min of values within the array and sorts on that, whereas the type by {"a.b.c":-1} takes the max of all of the values.