Вопрос

here is the output from my command-line. You can see that the first attempt to create an index fails. Then I just rename the loc-Field to coordinates and it works ??? Can somebody explain that to me. This seems like an strange behavior.

> db.nodes.find().limit(3)
{ "_id" : ObjectId("534d36b682beda5978db27c1"), "geo" : { "type" : "Point", "loc" : [ 9.7366511, 52.3711883 ] } }
{ "_id" : ObjectId("534d36b682beda5978db27c2"), "geo" : { "type" : "Point", "loc" : [ 9.7399576, 52.3691615 ] } }
{ "_id" : ObjectId("534d36b682beda5978db27c3"), "geo" : { "type" : "Point", "loc" : [ 9.7346094, 52.371738 ] } }
> db.nodes.ensureIndex({"geo":"2dsphere"})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "ok" : 0,
        "errmsg" : "Can't extract geo keys from object, malformed geometry?: { _id: ObjectId('534d36b682beda5978db27c1'), geo: { type: \"Point\", loc: [ 9.7366511, 52.3711883 ] } }",
        "code" : 16755
}
> db.nodes.find().limit(3)
{ "_id" : ObjectId("534d36ea82be11ca6e9fb112"), "geo" : { "type" : "Point", "coordinates" : [ 9.7366511, 52.3711883 ] } }
{ "_id" : ObjectId("534d36ea82be11ca6e9fb113"), "geo" : { "type" : "Point", "coordinates" : [ 9.7399576, 52.3691615 ] } }
{ "_id" : ObjectId("534d36ea82be11ca6e9fb114"), "geo" : { "type" : "Point", "coordinates" : [ 9.7346094, 52.371738 ] } }
> db.nodes.ensureIndex({"geo":"2dsphere"})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
Это было полезно?

Решение

It isn't clear why the second attempt fails silently, but the major problem here is that your json structure doesn't quite adhere to the GeoJSON specification. reference

A "Point" GeoJSON structure should have it's coordinates contained in a key called "coordinates"—not "loc".

Try creating the index with { "_id" : ObjectId("534d36b682beda5978db27c3"), "geo" : { "type" : "Point", "coordinates" : [ 9.7346094, 52.371738 ] } }

And remember that longitude is always first in the coordinates array! :]

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top