[SERVER-12633] Query: Cannot hint with geo $near Created: 05/Feb/14  Updated: 21/Jul/14  Resolved: 21/Jul/14

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

Type: Bug Priority: Minor - P4
Reporter: Jonathan Abrahams Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Operating System: ALL
Steps To Reproduce:

 
function getRandomLat(seed) {
	lat=Math.random(seed)*56666%180;
	if (Math.round(Math.random(seed)) == 0) {
		lat=lat*-1;
	}
	return lat;
}
 
function getRandomLng(seed) {
	lng=Math.random(seed)*56666%90;
	if (Math.round(Math.random(seed)) == 0) {
		lng=lng*-1;
	}
	return lng;
}
 
for (i=0; i< 100; i++) {
	doc = {a: i%5, b: i%9, c: i%7,
		geo: {lng : getRandomLng(i), lat : getRandomLat(i)}};
       t.insert(doc)
}
t.ensureIndex({a:1});
t.ensureIndex({geo: "2d"});
t.find({a:4, geo: {$near: [getRandomLng(50),getRandomLat(50)]}}).explain().cursor;
t.find({a:4, geo: {$near: [getRandomLng(50),getRandomLat(50)]}}).hint({a:1}).explain().cursor
 

Participants:

 Description   

Specifying a hint when doing a geo $near query fails with an error:

t.find({a:4, geo: {$near: [getRandomLng(50),getRandomLat(50)]}}).hint({geo: 1}).explain().cursor
2014-02-05T15:59:44.687-0500 error: {
	"$err" : "Unable to execute query: error processing query: ns=test.filters limit=0 skip=0\nTree: $and\n    a == 4.0\n    GEONEAR  field=geo maxdist=1.79769e+308 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: bad hint",
	"code" : 17007
> t.find({a:4, geo: {$near: [getRandomLng(50),getRandomLat(50)]}}).hint({a:1}).explain().cursor
2014-02-05T15:54:41.638-0500 error: {
	"$err" : "Unable to execute query: error processing query: ns=test.filters limit=0 skip=0\nTree: $and\n    a == 4.0 First: 0 notFirst: full path: a\n    GEONEAR  field=geo maxdist=1.79769e+308 isNearSphere=0 First: notFirst: full path: geo\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query",
	"code" : 17007
} at src/mongo/shell/query.js:131
> t.find({a:4, geo: {$near: [getRandomLng(50),getRandomLat(50)]}}).explain().cursor
GeoSearchCursor



 Comments   
Comment by Jonathan Abrahams [ 05/Feb/14 ]

Any hint fails, i.e.

 hint({geo: "2d"}) or hint({a: 1})  

Is there a restriction in hinting when using geo query? I didn't see this in the documentation. The only restriction I found was that the collection can only have one geo index on it.

Comment by Scott Hernandez (Inactive) [ 05/Feb/14 ]

So why do you think you can hint on another index? "... hint({geo: 1}) ..." is not valid given your indexes, which is not your hint. You might want to remove it from this issue since I don't think that is what this issue is about, is it?

Comment by Jonathan Abrahams [ 05/Feb/14 ]

As listed in the steps section, the indexes were created as:

t.ensureIndex({a:1});
t.ensureIndex({geo: "2d"});

Comment by Scott Hernandez (Inactive) [ 05/Feb/14 ]

Did you create an index on {geo:1} or using "2d"?

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