[SERVER-7915] Appending sort() executes $or operator with geospatial indexing when it should result in an user assert. Created: 12/Dec/12  Updated: 18/Aug/14  Resolved: 18/Aug/14

Status: Closed
Project: Core Server
Component/s: Geo, Querying
Affects Version/s: 2.4.10
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Mark porter Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

n/a


Issue Links:
Depends
Operating System: ALL
Participants:

 Description   

//insert some geo stuff in a "locations" collection

db.locations.insert( { name : "london", loc : [ 51.5073346, 0.12768310000001293 ] })
 
db.locations.insert( { name : "guildford", loc : [51.23622, 0.5704090000000406] })
 
db.locations.insert( { name : "farnborough", loc : [51.2868939, 0.7526149999999916] })
 
db.locations.insert( { name : "camberley", loc : [51.3353899, 0.7428559999999607] })
 
db.locations.insert( { name : "cheltenham", loc : [51.897599, 2.075608999999986] })
 
db.locations.insert( { name : "bristol", loc : [51.454513, 2.5879099999999653] })
 
db.locations.insert( { name : "reading", loc : [51.4528837, 0.9739059999999427] })

// simple geo find

db.locations.find({ loc : { $nearSphere : [51.2868, 0.7526], $maxDistance : 0.002523 } }) 

// here is an $OR, which fails correctly and generates the appropriate user assert:

db.locations.find({ 
        "$or" : [ 
                {
                        "loc" : { "$nearSphere" : [51.2868, 0.7526], "$maxDistance" : 0.002523 }
                }, 
                {
                        "name" : "London"
                } 
        ]
}) 

error: { "$err" : "$or may not contain 'special' query", "code" : 13291 }

Appending sort() results in the query being executed instead of the user assert (Code 13291) being generated.

 db.locations.find({         "$or" : [                  {                         "loc" : { "$nearSphere" : [51.2868, 0.7526], "$maxDistance" : 0.002523 }                 },                  {                         "name" : "London"                 }          ] }).sort({ "DoesNotExist" : 1 });
{ "_id" : ObjectId("50c8ce18a17a909731b0d384"), "name" : "london", "loc" : [ 51.5073346, 0.12768310000001293 ] }
{ "_id" : ObjectId("50c8ce18a17a909731b0d385"), "name" : "guildford", "loc" : [ 51.23622, 0.5704090000000406 ] }
{ "_id" : ObjectId("50c8ce18a17a909731b0d386"), "name" : "farnborough", "loc" : [ 51.2868939, 0.7526149999999916 ] }
{ "_id" : ObjectId("50c8ce18a17a909731b0d387"), "name" : "camberley", "loc" : [ 51.3353899, 0.7428559999999607 ] }
{ "_id" : ObjectId("50c8ce18a17a909731b0d388"), "name" : "cheltenham", "loc" : [ 51.897599, 2.075608999999986 ] }
{ "_id" : ObjectId("50c8ce18a17a909731b0d389"), "name" : "bristol", "loc" : [ 51.454513, 2.5879099999999653 ] }
{ "_id" : ObjectId("50c8ce19a17a909731b0d38a"), "name" : "reading", "loc" : [ 51.4528837, 0.9739059999999427 ] }

which based on the code does not appear to be correct behaviour.

mongo/src/mongo/db/queryutil.cpp

   _orSets.push_back( FieldRangeSetPair( ns, f.embeddedObject(), optimize ) );
                    uassert( 13291, "$or may not contain 'special' query", _orSets.back().getSpecial().empty() );
                    _originalOrSets.push_back( _orSets.back() );



 Comments   
Comment by Thomas Rueckstiess [ 18/Aug/14 ]

This issue is no longer present and fixed as of 2.6. Due to its low impact I'm closing this ticket as Won't Fix.

Comment by Thomas Rueckstiess [ 18/Aug/14 ]

The issue was present in 2.4 but seems fixed in 2.6. Both versions (with and without sort return the error).

> db.locations.find({"$or":[{"loc":{"$nearSphere":[51.2868,0.7526],"$maxDistance":0.002523}},{"name":"London"}]})
error: {
	"$err" : "Can't canonicalize query: BadValue geoNear must be top-level expr",
	"code" : 17287
}
> db.locations.find({"$or":[{"loc":{"$nearSphere":[51.2868,0.7526],"$maxDistance":0.002523}},{"name":"London"}]}).sort({foo: 1})
error: {
	"$err" : "Can't canonicalize query: BadValue geoNear must be top-level expr",
	"code" : 17287
}

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