replset:PRIMARY> db.cs22636.find().sort({"xyz.a":1})
// This is the natural order that is displayed, instead of the sorted one
{ "_id" : ObjectId("55a902338943286a2df81a06"), "xyz" : [ { "a" : 1323 }, { "b" : 1 } ] }
{ "_id" : ObjectId("55a902398943286a2df81a07"), "xyz" : [ { "a" : 2 }, { "b" : 3 } ] }
{ "_id" : ObjectId("55a9023f8943286a2df81a08"), "xyz" : [ { "a" : 88 }, { "b" : 0 } ] }
{ "_id" : ObjectId("55a903778943286a2df81a09"), "xyz" : [ { "a" : 4325 }, { "b" : 0 } ] }
{ "_id" : ObjectId("55a9037a8943286a2df81a0a"), "xyz" : [ { "a" : 4325435 }, { "b" : 0 } ] }
{ "_id" : ObjectId("55a9037e8943286a2df81a0b"), "xyz" : [ { "a" : 43 }, { "b" : 0 } ] }
// Going backward works
replset:PRIMARY> db.cs22636.find().sort({"xyz.a":-1})
{ "_id" : ObjectId("55a9037a8943286a2df81a0a"), "xyz" : [ { "a" : 4325435 }, { "b" : 0 } ] }
{ "_id" : ObjectId("55a903778943286a2df81a09"), "xyz" : [ { "a" : 4325 }, { "b" : 0 } ] }
{ "_id" : ObjectId("55a902338943286a2df81a06"), "xyz" : [ { "a" : 1323 }, { "b" : 1 } ] }
{ "_id" : ObjectId("55a9023f8943286a2df81a08"), "xyz" : [ { "a" : 88 }, { "b" : 0 } ] }
{ "_id" : ObjectId("55a9037e8943286a2df81a0b"), "xyz" : [ { "a" : 43 }, { "b" : 0 } ] }
{ "_id" : ObjectId("55a902398943286a2df81a07"), "xyz" : [ { "a" : 2 }, { "b" : 3 } ] }
// Adding a first index, and hoping it will use it.
db.cs22636.ensureIndex({"xyz.a":1})
// Getting the same results, forward and backward as without index
replset:PRIMARY> db.cs22636.find().sort({"xyz.a":1}).hint({"xyz.a":1})
{ "_id" : ObjectId("55a902338943286a2df81a06"), "xyz" : [ { "a" : 1323 }, { "b" : 1 } ] }
{ "_id" : ObjectId("55a902398943286a2df81a07"), "xyz" : [ { "a" : 2 }, { "b" : 3 } ] }
{ "_id" : ObjectId("55a9023f8943286a2df81a08"), "xyz" : [ { "a" : 88 }, { "b" : 0 } ] }
{ "_id" : ObjectId("55a903778943286a2df81a09"), "xyz" : [ { "a" : 4325 }, { "b" : 0 } ] }
{ "_id" : ObjectId("55a9037a8943286a2df81a0a"), "xyz" : [ { "a" : 4325435 }, { "b" : 0 } ] }
{ "_id" : ObjectId("55a9037e8943286a2df81a0b"), "xyz" : [ { "a" : 43 }, { "b" : 0 } ] }
replset:PRIMARY> db.cs22636.find().sort({"xyz.a":-1}).hint({"xyz.a":1})
{ "_id" : ObjectId("55a9037a8943286a2df81a0a"), "xyz" : [ { "a" : 4325435 }, { "b" : 0 } ] }
{ "_id" : ObjectId("55a903778943286a2df81a09"), "xyz" : [ { "a" : 4325 }, { "b" : 0 } ] }
{ "_id" : ObjectId("55a902338943286a2df81a06"), "xyz" : [ { "a" : 1323 }, { "b" : 1 } ] }
{ "_id" : ObjectId("55a9023f8943286a2df81a08"), "xyz" : [ { "a" : 88 }, { "b" : 0 } ] }
{ "_id" : ObjectId("55a9037e8943286a2df81a0b"), "xyz" : [ { "a" : 43 }, { "b" : 0 } ] }
{ "_id" : ObjectId("55a902398943286a2df81a07"), "xyz" : [ { "a" : 2 }, { "b" : 3 } ] }
// not putting "b" in the array of values
// results are fine, forward and backward
replset:PRIMARY> db.cs22636c.find().sort({"xyz.a":1})
{ "_id" : ObjectId("55a9a13a978c4d7510ae5a47"), "xyz" : [ { "a" : 2 } ] }
{ "_id" : ObjectId("55a9a157978c4d7510ae5a4b"), "xyz" : [ { "a" : 43 } ] }
{ "_id" : ObjectId("55a9a13f978c4d7510ae5a48"), "xyz" : [ { "a" : 88 } ] }
{ "_id" : ObjectId("55a9a131978c4d7510ae5a46"), "xyz" : [ { "a" : 1323 } ] }
{ "_id" : ObjectId("55a9a147978c4d7510ae5a49"), "xyz" : [ { "a" : 4325 } ] }
{ "_id" : ObjectId("55a9a151978c4d7510ae5a4a"), "xyz" : [ { "a" : 4325435 } ] }
replset:PRIMARY> db.cs22636c.find().sort({"xyz.a":-1})
{ "_id" : ObjectId("55a9a151978c4d7510ae5a4a"), "xyz" : [ { "a" : 4325435 } ] }
{ "_id" : ObjectId("55a9a147978c4d7510ae5a49"), "xyz" : [ { "a" : 4325 } ] }
{ "_id" : ObjectId("55a9a131978c4d7510ae5a46"), "xyz" : [ { "a" : 1323 } ] }
{ "_id" : ObjectId("55a9a13f978c4d7510ae5a48"), "xyz" : [ { "a" : 88 } ] }
{ "_id" : ObjectId("55a9a157978c4d7510ae5a4b"), "xyz" : [ { "a" : 43 } ] }
{ "_id" : ObjectId("55a9a13a978c4d7510ae5a47"), "xyz" : [ { "a" : 2 } ] }