[SERVER-7581] geo query with $elemMatch not working with $near Created: 07/Nov/12  Updated: 06/Dec/22  Resolved: 05/Jul/19

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

Type: Bug Priority: Major - P3
Reporter: raxit sheth Assignee: Backlog - Query Team (Inactive)
Resolution: Done Votes: 3
Labels: geo, query-44-grooming
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

all


Assigned Teams:
Query
Operating System: ALL
Participants:

 Description   

> db.revdirection.find({"address" : {$elemMatch:{"loc":[72.81668,18.90649],name:"end1"}}},{address:1})
{ "_id" : ObjectId("50978c078ef31a13c9000019"), "address" : [ 	{ 	"loc" : [ 	72.83017000000001, 	19.0035 ], 	"name" : "start1" }, 	{ 	"loc" : [ 	72.81668, 	18.90649 ], 	"name" : "end1" } ] }
> db.revdirection.find({"address" : {$elemMatch:{"loc":[72.81668,18.90649]}}},{address:1})
{ "_id" : ObjectId("50978baa8ef31a13a2000019"), "address" : [ 	{ 	"loc" : [ 	72.83017000000001, 	19.0035 ], 	"name" : "end1" }, 	{ 	"loc" : [ 	72.81668, 	18.90649 ], 	"name" : "start1" } ] }
{ "_id" : ObjectId("50978c078ef31a13c9000019"), "address" : [ 	{ 	"loc" : [ 	72.83017000000001, 	19.0035 ], 	"name" : "start1" }, 	{ 	"loc" : [ 	72.81668, 	18.90649 ], 	"name" : "end1" } ] }
{ "_id" : ObjectId("509791028ef31a1474000019"), "address" : [ 	{ 	"loc" : [ 	72.83017000000001, 	19.0035 ], 	"name" : "start" }, 	{ 	"loc" : [ 	72.81668, 	18.90649 ], 	"name" : "end" } ] }
> 
> 
> db.revdirection.find({"address" : {$elemMatch:{"loc":{$near:[72.81668,18.90649]},name:"end1"}}},{address:1})
error: {
	"$err" : "can't find special index: 2d for: { address: { $elemMatch: { loc: { $near: [ 72.81668000000001, 18.90649 ] }, name: \"end1\" } } }",
	"code" : 13038
}
> 



 Comments   
Comment by David Storch [ 05/Jul/19 ]

Nesting $near beneath $elemMatch has never been supported, to my knowledge. In more recent versions, the error message states this more clearly:

> db.revdirection.find({"address" : {$elemMatch:{"loc":{$near:[72.81668,18.90649]},name:"end1"}}},{address:1})
Error: error: {
	"ok" : 0,
	"errmsg" : "geoNear must be top-level expr",
	"code" : 2,
	"codeName" : "BadValue"
}

Closing as Works as Designed.

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