Common issues with MongoDB's geo-spatial index

Source: Internet
Author: User
Tags mongodb version

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.