Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB:Can't canonicalize query: BadValue bad geo query

I indexed the field loc on 2dsphere and i am unable to run the geowithin query on Point type GeoJson data.

Here is the query:

 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 ]
                   },

         }

Indexes:

{
"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 on the same documents. I am trying to understand why geowithin is not working ?

like image 507
Swakesh Avatar asked Sep 17 '14 14:09

Swakesh


1 Answers

It is because your polygon is not closed, you actually need a minimum of four points for a valid polygon, with the first point repeated at the end, see the GeoJSON Polygon docs. The error message could be a bit more helpful, it has to be said. This is also true of the Well Known Text (WKT) and Well Known Binary (WKB) polygon formats, so is not a peculiarity of GeoJSON.

Your query should work like this:

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]]]                                     
                        } 
                    } 
                 } 
             } 
like image 88
John Powell Avatar answered Sep 22 '22 00:09

John Powell