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

$orderby meta operator allows an array, which it ignores

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Trivial - P5 Trivial - P5
    • None
    • Affects Version/s: 3.0.4
    • Component/s: Querying
    • None
    • ALL

      I'm not sure why an array is accepted as an $orderby value, since it appears to have no effect on the query. I thought that there might have been some lax checking around the array/document field types and MongoDB would simply use the key/value pairs regardless (e.g. [1] would be equivalent to {"0":1}), but that wasn't the case. Additionally, I checked if an array of field names would be accepted, with an implied ascending order.

      > db.foo.drop()
      true
      > db.foo.insert([{0:5},{0:4},{0:3}])
      BulkWriteResult(...)
      > db.foo.find({$query:{},$orderby:[-1]})
      { "_id" : ObjectId("559ad6b5251b88948583bb1c"), "0" : 5 }
      { "_id" : ObjectId("559ad6b7251b88948583bb1d"), "0" : 4 }
      { "_id" : ObjectId("559ad6b8251b88948583bb1e"), "0" : 3 }
      > db.foo.find({$query:{},$orderby:[1]})
      { "_id" : ObjectId("559ad6b5251b88948583bb1c"), "0" : 5 }
      { "_id" : ObjectId("559ad6b7251b88948583bb1d"), "0" : 4 }
      { "_id" : ObjectId("559ad6b8251b88948583bb1e"), "0" : 3 }
      > db.foo.find({$query:{},$orderby:["0"]})
      { "_id" : ObjectId("559ad6b5251b88948583bb1c"), "0" : 5 }
      { "_id" : ObjectId("559ad6b7251b88948583bb1d"), "0" : 4 }
      { "_id" : ObjectId("559ad6b8251b88948583bb1e"), "0" : 3 }
      > db.foo.find({$query:{},$orderby:{0:-1}})
      { "_id" : ObjectId("559ad6b5251b88948583bb1c"), "0" : 5 }
      { "_id" : ObjectId("559ad6b7251b88948583bb1d"), "0" : 4 }
      { "_id" : ObjectId("559ad6b8251b88948583bb1e"), "0" : 3 }
      > db.foo.find({$query:{},$orderby:{0:1}})
      { "_id" : ObjectId("559ad6b8251b88948583bb1e"), "0" : 3 }
      { "_id" : ObjectId("559ad6b7251b88948583bb1d"), "0" : 4 }
      { "_id" : ObjectId("559ad6b5251b88948583bb1c"), "0" : 5 }
      > db.foo.find({$query:{},$orderby:5})
      Error: error: {
      	"$err" : "Can't canonicalize query: BadValue sort must be object or array",
      	"code" : 17287
      }
      

      This may end up being redundant in light of SERVER-15176, especially if we'd rather not make the $orderby validation stricter in a 3.0.x patch release.

            Assignee:
            Unassigned Unassigned
            Reporter:
            jmikola@mongodb.com Jeremy Mikola
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: