[SERVER-3297] Bounding box queries in embedded geo documents (Mongo v1.9) not working properly Created: 20/Jun/11  Updated: 30/Mar/12  Resolved: 21/Jun/11

Status: Closed
Project: Core Server
Component/s: Geo
Affects Version/s: 1.9.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: ramesh vel Assignee: Greg Studer
Resolution: Done Votes: 0
Labels: geo, indexing, query
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Platform independent


Operating System: Linux
Participants:

 Description   

Bounding box queries inside the embedded documents are giving inaccurate results. Sample document

"_id" : ObjectId("4dff052e1d41c8588000000a"),
"isused" : true,
"isfixed" : true,
"kind" : 0,
"name" : "Used whirlpool refrigirator wanted",
"desc" : "2 years used 200 litres Whirlpool refrigerator is for sale.",
"price" : 5000,
"locations" : [

{ "name" : "Samiyar Madam, Kodambakkam, Chennai, Tamil Nadu, India", "loc" : [ 80.22167450000006, 13.0454044 ], "is_exact" : true, "kind" : "neighborhood", "_id" : ObjectId("4dff052e1d41c8588000000b") }

,

{ "_id" : ObjectId("4dff052e1d41c8588000000c"), "kind" : "route", "name" : "Shiyamala Vadhana St, Samiyar Madam, Kodambakkam, Chennai, Tamil Nadu, India", "loc" : [ 80.2216906, 13.0452927 ] }

,

{ "_id" : ObjectId("4dff052e1d41c8588000000d"), "kind" : "sublocality", "name" : "Kodambakkam, Chennai, Tamil Nadu, India", "loc" : [ 80.223766, 13.049043 ] }

,
}]
}

when issuing the $box query with this co-ordinates [[80.21758681346137, 13.04706625300893], [80.2238953690644, 13.052333939665253]], it should not pick the above item

db.items.find({"locations.is_exact":true,"locations.loc" : {"$within" :

{"$box" :[[80.21758681346137, 13.04706625300893], [80.2238953690644, 13.052333939665253]] }

}})

But this query picks the above item.

So i created another document(not embedded) with the same data & location. like this

"_id" : ObjectId("4dff052e1d41c8588000000a"),
"isused" : true,
"isfixed" : true,
"kind" : 0,
"name" : "Used whirlpool refrigirator wanted",
"desc" : "2 years used 200 litres Whirlpool refrigerator is for sale.",
"price" : 5000,
"loc" : [
80.22167450000006,
13.0454044
]

and used the same box query

db.items.find({loc : {"$within" :

{"$box" :[[80.21758681346137, 13.04706625300893], [80.2238953690644, 13.052333939665253]] }

}})

now it works fine. it doesn't pick up the item. So this problem only exists in v1.9 geo indexes in embedded documents



 Comments   
Comment by ramesh vel [ 24/Jun/11 ]

Greg,
thanks for the clarification. Now its clear to me. sure i ll vote for the feature.

Comment by Greg Studer [ 24/Jun/11 ]

I don't mean to offend you or minimize your issue, we just tend to be concise.

If you have a coll with a non-geo document like:
{ loc : [

{ is_exact : true, loc : 0 }

,

{ is_exact : false, loc : 1 }

] }

and you search for:
db.coll.find(

{ "loc.is_exact" : true, "loc.loc" : 1 }

)
you'll get back the document, the match applies to the entire document, not the sub-documents separately :
{ loc : [

{ is_exact : true, loc : 0 }

,

{ is_exact : false, loc : 1 }

] }

In order to do individual subdocument matching, you need to use the $elemMatch operator:
db.coll.find({ loc : { $elemMatch :

{ is_exact : true, loc : 1 }

} })

This operator isn't yet supported by geospatial queries - I've created an improvement ticket that you can vote on if you think it should be:
https://jira.mongodb.org/browse/SERVER-3325

Comment by ramesh vel [ 22/Jun/11 ]

Greg Studer,
No its not working correctly. If you look at the query correctly i have used one more filter "locations.is_exact" : true.

db.items.find({"locations.is_exact":true,"locations.loc" : {"$within" :

{"$box" :[[80.21758681346137, 13.04706625300893], [80.2238953690644, 13.052333939665253]] }

}})

So the query must look only into the first embedded document

"name" : "Samiyar Madam, Kodambakkam, Chennai, Tamil Nadu, India",
"loc" : [
80.22167450000006,
13.0454044
],
"is_exact" : true,
"kind" : "neighborhood",
"_id" : ObjectId("4dff052e1d41c8588000000b")
},

not the one you have mentioned [ 80.223766, 13.049043 ] one.

{
"_id" : ObjectId("4dff052e1d41c8588000000d"),
"kind" : "sublocality",
"name" : "Kodambakkam, Chennai, Tamil Nadu, India",
"loc" : [
80.223766,
13.049043
]
},

Also, if it works perfectly, it should work in both normal & embedded documents. But its not. So its an issue.

So please re-open the issue, its a important one.

Comment by Greg Studer [ 21/Jun/11 ]

Think this is working properly? The third point:

[ 80.223766, 13.049043 ]

is inside the box:

[[80.21758681346137, 13.04706625300893], [80.2238953690644, 13.052333939665253]]

I'm guessing you're trying to search for documents with all points within a region? - $within queries find documents with any points inside a region, right now there's no automatic way to get those results, you'd have to filter them on the client side. If this is what you were expecting and it's important for your use case, I'd recommend creating (a different) improvement JIRA case.

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