[SERVER-12929] Improve error message for 2dsphere $near query with extra fields in geo predicate Created: 27/Feb/14  Updated: 05/Jul/19  Resolved: 07/Mar/14

Status: Closed
Project: Core Server
Component/s: Geo
Affects Version/s: 2.6.0-rc0
Fix Version/s: 2.6.0-rc2

Type: Bug Priority: Major - P3
Reporter: Derick Rethans Assignee: Benety Goh
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-11832 Misplaced $maxDistance can work in v2... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Participants:

 Description   

2dsphere $near queries return the unhelpful message "not handled: $near" on queries if extra fields are erroneously included in the geo predicate:

> db.poiConcat.ensureIndex({l: "2dsphere"})
WriteResult({ "nInserted" : 1 })
> db.poiConcat.insert({l: {type: 'Point', coordinates: [ -73.56, 45.49 ] }, ts: 'amenity=pub'})
WriteResult({ "nInserted" : 1 })
> db.poiConcat.find( { l: { '$near': { '$geometry': { type: 'Point', coordinates: [ -73.56, 45.48] } } }, 'ts': 'amenity=pub' } ).limit(1).pretty();
{
	"_id" : ObjectId("530f897fe931f3ea33c3fca2"),
	"l" : {
		"type" : "Point",
		"coordinates" : [
			-73.56,
			45.49
		]
	},
	"ts" : "amenity=pub"
}
> db.poiConcat.find( { l: { '$near': { '$geometry': { type: 'Point', coordinates: [ -73.56, 45.48] } }, foo: 'bar' }, 'ts': 'amenity=pub' } ).limit(1).pretty();
error: {
	"$err" : "Can't canonicalize query: BadValue not handled: $near",
	"code" : 17287
}

This error message needs to be improved, particularly because the below query form was a (undocumented but) valid way to specify $maxDistance for a $near query in 2.4. This form is being broken for 2.6, but users who use the invalid form need better diagnostics.

> db.poiConcat.find( { l: { '$near': { '$geometry': { type: 'Point', coordinates: [ -73.56, 45.48] } }, $maxDistance: 100 }, 'ts': 'amenity=pub' } ).limit(1).pretty();
error: {
	"$err" : "Can't canonicalize query: BadValue not handled: $near",
	"code" : 17287
}

Original ticket description:

With 2.4.x (and I also believe 2.5.5), I could run the following query:

db.poiConcat.find( { l: { '$near': { '$geometry': { type: 'Point', coordinates: [ -73.56, 45.49 ] } }, '$maxDistance' : 2500 }, 'ts': 'amenity=pub' } ).limit(1).pretty();

With 2.6.0rc0, this now returns:

error: {
	"$err" : "Can't canonicalize query: BadValue not handled: $near",
	"code" : 17287
}

Steps to reproduce: No data or indexes have to be created. Just run this:

derick@whisky:~ $ install/mongodb-linux-x86_64-2.6.0-rc0/bin/mongo foo
MongoDB shell version: 2.6.0-rc0
connecting to: foo
Server has startup warnings: 
2014-02-27T09:25:14.228-0500 ** WARNING: --rest is specified without --httpinterface,
2014-02-27T09:25:14.228-0500 **          enabling http interface
> db.poiConcat.find( { l: { '$near': { '$geometry': { type: 'Point', coordinates: [ -73.56, 45.49 ] } }, '$maxDistance' : 2500 }, 'ts': 'amenity=pub' } ).limit(1).pretty();
error: {
	"$err" : "Can't canonicalize query: BadValue not handled: $near",
	"code" : 17287
}



 Comments   
Comment by Githook User [ 07/Mar/14 ]

Author:

{u'username': u'benety', u'name': u'Benety Goh', u'email': u'benety@mongodb.com'}

Message: SERVER-12929 propage error from GeoQuery::parseNewQuery to expression parser
Branch: master
https://github.com/mongodb/mongo/commit/18b7f9c5f43556ecde34883ee046bdde16f0cd1c

Comment by Daniel Pasette (Inactive) [ 02/Mar/14 ]

error message should be improved if possible.

Comment by J Rassi [ 27/Feb/14 ]

derick: you put $maxDistance in the top level of the predicate on 'l' by accident:

> db.poiConcat.insert({l: {type: 'Point', coordinates: [ -73.56, 45.49 ] }, ts: 'amenity=pub'})
WriteResult({ "nInserted" : 1 })
> db.poiConcat.ensureIndex({l: "2dsphere"})
WriteResult({ "nInserted" : 1 })
> db.poiConcat.find( { l: { '$near': { '$geometry': { type: 'Point', coordinates: [ -73.56, 45.49 ] } }, '$maxDistance' : 2500 }, 'ts': 'amenity=pub' } ).limit(1).pretty();
error: {
	"$err" : "Can't canonicalize query: BadValue not handled: $near",
	"code" : 17287
}
> db.poiConcat.find( { l: { '$near': { '$geometry': { type: 'Point', coordinates: [ -73.56, 45.49 ] }, '$maxDistance' : 2500 } }, 'ts': 'amenity=pub' } ).limit(1).pretty();
{
	"_id" : ObjectId("530f828d277f18f878479cee"),
	"l" : {
		"type" : "Point",
		"coordinates" : [
			-73.56,
			45.49
		]
	},
	"ts" : "amenity=pub"
}
>

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