Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-19497

Sort is not done on embedded field

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.6.10, 3.0.2
    • Component/s: Querying
    • None
    • ALL
    • Hide
      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 } ] }
      
      Show
      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 } ] }

      A sort operation is not happening on a sub field.
      I reproduced it in 2.6.10, while a customer saw it in 3.0.2.

            Assignee:
            Unassigned Unassigned
            Reporter:
            daniel.coupal@mongodb.com Daniel Coupal
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: