Show
Here's the REPL transcript.
> 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 ] ] ] } }