Details
-
Bug
-
Resolution: Duplicate
-
Trivial - P5
-
None
-
3.0.4
-
None
-
ALL
Description
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.
Attachments
Issue Links
- duplicates
-
SERVER-18905 Server should reject array values for sort specification
-
- Closed
-