Considerations for creating geo-spatial indexes
The creation of the geospatial index failed with the following error message
> Db.places.ensureIndex ({"loc": "2dsphere"})
{
"Createdcollectionautomatically": false,
"Numindexesbefore": 1,
"OK": 0,
"ErrMsg": "Can ' t extract Geo keys from object, malformed geometry?: {_
Id:objectid (' 5428bb224f26d9aa1af3d844 '), name: \ "Hudson river\", loc: {type: \
"Line\", coordinates: [[0.0, 1.0], [0.0, 2.0], [1.0, 2.0]}} ",
"Code": 16755
}
The reason is that the polygon has a minimum of 4 points to create a successful
MongoDB version: mongodb-2.6.
> Db.world.insert ({"Name": "New England", "loc": {"type": "Polygon", "
Coordinates ": [[[0,1],[0,2],[1,2]]}}) There are only 3 points, the outer side of which should have two [and]
> Db.world.insert ({"Name": "New England", "loc": {"type": "Polygon", "
Coordinates ": [[[[[0,1],[0,2],[1,2],[0,1]]}}) There are 4 of them.
Create a success message as follows:
> Db.places.ensureIndex ({"loc": "2dsphere"})
{
"Createdcollectionautomatically": false,
"Numindexesbefore": 1,
"Numindexesafter": 2,
"OK": 1
}
This error is generally JSON format and incorrect content.
The error message is as follows
"$err": "Can ' t canonicalize query:badvalue bad Geo query"
The reason is that the Type=polygon type, which represents a polygon, requires at least 4 points (the start and end point must be the same), and the polygon has multiple rings, so it should be an array structure, and each array is a polygon.
The correct format is as follows:
Db.places.insert({"name": "New England", "loc":{"type": "Polygon", "coordinates":[[[0,1] , [0,2],[1,2],[0,1]]}})
var eastvillage={"type": "Polygon", "coordinates": [[ -73.9917900,40.7264100],[-73.9917900,40.7321400],[- 73.9829300,40.7321400],[-73.9917900,40.7264100]]}
The reference documentation is as follows:
Http://stackoverflow.com/questions/25893415/mongodbcant-canonicalize-query-badvalue-bad-geo-query
Http://geojson.org/geojson-spec.html#id4
Common issues with MongoDB's geo-spatial index