this query:
db.getCollection('test_sort').find({}).sort({'sort_field': 1})
yields this result:
/* 1 */
{
"_id" : ObjectId("55ca4705824fb4055859fc3d"),
"sort_field" : [
0,
".",
55
]
}
/* 2 */
{
"_id" : ObjectId("55ca4716824fb4055859fc3e"),
"sort_field" : [
0,
".",
1
]
}
/* 3 */
{
"_id" : ObjectId("55ca471e824fb4055859fc3f"),
"sort_field" : [
0,
".",
22
]
}
/* 4 */
{
"_id" : ObjectId("55ca4726824fb4055859fc40"),
"sort_field" : [
0,
".",
84
]
}
/* 5 */
{
"_id" : ObjectId("55ca472d824fb4055859fc41"),
"sort_field" : [
0,
".",
12
]
}
This doesn't seem right. Does MongoDB ignore the contents of the array? It doesn't seem to, in other tests it seems to work as expected.
Based on gbot's answer, in the case of ascending, the smallest member of the array is used. Are there any workarounds for this? It's ok if the workaround requires a specific length of array, e.g. {"sort_field.0": 1, "sort_field.1": 1, "sort_field.2": 1} (my example doesn't seem to work either btw)