[SERVER-19294] $orderby meta operator allows an array, which it ignores Created: 06/Jul/15  Updated: 06/Jul/15  Resolved: 06/Jul/15

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 3.0.4
Fix Version/s: None

Type: Bug Priority: Trivial - P5
Reporter: Jeremy Mikola Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-18905 Server should reject array values for... Closed
Operating System: ALL
Participants:

 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.



 Comments   
Comment by J Rassi [ 06/Jul/15 ]

I confirm that this is a dup of SERVER-18905 (see my June 16 comment from that ticket for the history of this behavior). Closing as such.

Generated at Thu Feb 08 03:50:29 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.