MongoDB: Cannot canonicalize request: Bad geo-request BadValue

I indexed the loc field on 2dsphere and I cannot run a geowithin query for data of type GeoJson of type Point.

Here is the request:

db.test.find( { loc : { $geoWithin : { $geometry : { type : "Polygon" , coordinates : [ [ [-74.6862705412253, 40.42341005] , [-75.0846179, 39.9009465 ], [-74.20570119999999, 41.0167639 ] ] ] } } } } 

Output:

  uncaught exception: error: { "$err" : "Can't canonicalize query: BadValue bad geo query", "code" : 17287 } 

Document structure:

  { "_id" : ObjectId("53d15e7132e7b7978c472e6e"), "loc" : { "type" : "Point", "coordinates" : [ -74.6862705412253, 40.42341005 ] }, } 

Indices:

 { "0" : { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "collab.test" }, "1" : { "v" : 1, "key" : { "loc" : "2dsphere" }, "name" : "TestLocationIndex", "ns" : "collab.test", "2dsphereIndexVersion" : 2 } 

}

But, $ Polygon works fine in the same docs. I'm trying to understand why geyvitin does not work?

+6
source share
1 answer

This is because your polygon is not closed, you really need at least four points for a real polygon, with the first point repeated at the end, see GeoJSON Polygon Docs . The error message may be a little more useful, it must be said. This is also true for Famous Text (WKT) and well-known binary (WKB) formats, so this is not a feature of GeoJSON.

Your request should work as follows:

 db.test.find({loc : {$geoWithin : {$geometry : {type : "Polygon" , coordinates : [[[-74.6862705412253, 40.42341005] , [-75.0846179, 39.9009465], [-74.20570119999999, 41.0167639], [-74.6862705412253, 40.42341005]]] } } } } 
+13
source

Source: https://habr.com/ru/post/975430/


All Articles