Confused about geoWithin query.

83 views
Skip to first unread message

jfgo...@hivemapper.com

unread,
Apr 17, 2015, 9:23:57 PM4/17/15
to mongod...@googlegroups.com
Anyone know why these geoWithin queries don't behave as expected? I think I'm just missing something simple. I tried this on Mongo versions 2.6 and 3.

> use test
switched to db test
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
> db.test.createIndex({grid:'2dsphere'})
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
// make a square, coordinates start at NW corner
> db.test.insert({grid:{type:'Polygon',coordinates:[[[1,2],[2,2],[2,1],[1,1],[1,2]]]}})
WriteResult({ "nInserted" : 1 })
// find the square inside the square
> db.test.find({grid: { $geoWithin: { $geometry: { type: "Polygon", coordinates: [[[1,2],[2,2],[2,1],[1,1],[1,2]]] } } } })
{ "_id" : ObjectId("5531aa43fb4a314ff06590d9"), "grid" : { "type" : "Polygon", "coordinates" : [ [ [ 1, 2 ], [ 2, 2 ], [ 2, 1 ], [ 1, 1 ], [ 1, 2 ] ] ] } }
// square is missing from various rectangles including the square but extending in different directions, $geoIntersects works
> db.test.find({grid: { $geoWithin: { $geometry: { type: "Polygon", coordinates: [[[0,2],[2,2],[2,1],[0,1],[0,2]]] } } } })
> db.test.find({grid: { $geoWithin: { $geometry: { type: "Polygon", coordinates: [[[0,2],[3,2],[3,1],[0,1],[0,2]]] } } } })
> db.test.find({grid: { $geoWithin: { $geometry: { type: "Polygon", coordinates: [[[0,3],[3,3],[3,1],[0,1],[0,3]]] } } } })
// square is present in larger square
> db.test.find({grid: { $geoWithin: { $geometry: { type: "Polygon", coordinates: [[[0,3],[3,3],[3,0],[0,0],[0,3]]] } } } })
{ "_id" : ObjectId("5531aa43fb4a314ff06590d9"), "grid" : { "type" : "Polygon", "coordinates" : [ [ [ 1, 2 ], [ 2, 2 ], [ 2, 1 ], [ 1, 1 ], [ 1, 2 ] ] ] } }

Appreciate any help!

- John

Asya Kamsky

unread,
Apr 19, 2015, 5:12:40 PM4/19/15
to mongodb-user

Hi John,

The geoWithin doc page says that an object has to be *entirely* contained to satisfy the query.

http://docs.mongodb.org/manual/reference/operator/query/geoWithin/

Are you thinking of it in reverse (contains vs contained in)?

Asya

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/037cf0b4-55e1-493b-be44-7a6da5e1f5a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jfgo...@hivemapper.com

unread,
Apr 20, 2015, 1:25:50 PM4/20/15
to mongod...@googlegroups.com
Hi Asya, yes, I do understand that the document must be entirely within the shape in the query. Isn't that the case here?

I.e. isn't this square in the doc

{type:'Polygon',coordinates:[[[1,2],[2,2],[2,1],[1,1],[1,2]]]}

entirely contained in this rectangle in the query

{ type: "Polygon", coordinates: [[[0,2],[2,2],[2,1],[0,1],[0,2]]] }

so that the query should return the doc? But as the console output below shows, the query does not return it.

Asya Kamsky

unread,
Apr 24, 2015, 3:38:16 AM4/24/15
to mongod...@googlegroups.com
Looks like they share some coordinates.  I don't believe that containment allows for intersection...
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/34b12bbf-d6ac-4f10-8322-5008a93ee7c2%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
MongoDB World is back! June 1-2 in NYC. Use code ASYA for 25% off!

Reply all
Reply to author
Forward
0 new messages