MongoDB 2dsphere 空间索引创建错误

MongoDB 2dsphere spatial index creation error

在 MongoDB 中,我创建了一个名为 'GIS' 的数据库和一个名为 'UTILITIES' 的集合,并在集合中插入了一个 BSON 文档,如下所示。

{ 
"_id" : ObjectId("54e6b3ca7e550c1f4c2b47f6"), 
"features" : [
    {
        "id" : "1", 
        "geometry" : {
            "coordinates" : [
                86.74957, 
                21.93157
            ], 
            "type" : "Point", 
            "crs" : null, 
            "bbox" : [
                86.74957, 
                21.93157, 
                86.74957, 
                21.93157
            ]
        }, 
        "properties" : {
            "PLACE" : "Abhoy Medical Store", 
            "LATITUDE" : "21.93157", 
            "LONGITUDE" : "86.74957", 
            "IMG" : "43400012"
        }, 
        "type" : "Feature", 
        "crs" : null, 
        "bbox" : [
            86.74957, 
            21.93157, 
            86.74957, 
            21.93157
        ]
    }, 
    {
        "id" : "2", 
        "geometry" : {
            "coordinates" : [
                86.73604, 
                21.92578
            ], 
            "type" : "Point", 
            "crs" : null, 
            "bbox" : [
                86.73604, 
                21.92578, 
                86.73604, 
                21.92578
            ]
        }, 
        "properties" : {
            "PLACE" : "Advanced Homeo Sadan", 
            "LATITUDE" : "21.92578", 
            "LONGITUDE" : "86.73604", 
            "IMG" : "43400123"
        }, 
        "type" : "Feature", 
        "crs" : null, 
        "bbox" : [
            86.73604, 
            21.92578, 
            86.73604, 
            21.92578
        ]
    }
    ], 
"type" : "FeatureCollection", 
"crs" : null, 
"bbox" : [
    86.71509, 
    21.91163, 
    86.79117, 
    21.95772
]
}

该文档实际上是一个 GeoJSON。 我尝试将索引创建为

db.UTILITIES.ensureIndex ({"features.geometry" : "2dsphere"})

哪个 returns errmsg

{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"ok" : 0,
"errmsg" : "Can't extract geo keys from object, malformed geometry?: { _id: ObjectId('54e6b3ca7e550c1f4c2b47f6'), features: [ { id: \"1\", geometry: { coordinates: [ 86.74957000000001, 21.93157 ], type: \"Point\", crs: null, bbox: [ 86.74957000000001, 21.93157, 86.74957000000001, 21.93157 ] }, properties: { PLACE: \"Abhoy Medical Store\", LATITUDE: \"21.93157\", LONGITUDE: \"86.74957\", IMG: \"43400012\" }, type: \"Feature\", crs: null, bbox: [ 86.74957000000001, 21.93157, 86.74957000000001, 21.93157 ] }, { id: \"2\", geometry: { coordinates: [ 86.73604, 21.92578 ], type: \"Point\", crs: null, bbox: [ 86.73604, 21.92578, 86.73604, 21.92578 ] }, properties: { PLACE: \"Advanced Homeo Sadan\", LATITUDE: \"21.92578\", LONGITUDE: \"86.73604\", IMG: \"43400123\" }, type: \"Feature\", crs: null, bbox: [ 86.73604, 21.92578, 86.73604, 21.92578 ] } ], type: \"FeatureCollection\", crs: null, bbox: [ 86.71509, 21.91163, 86.79116999999999, 21.95772 ] }",
"code" : 16755
}

我检查了经纬度顺序没问题

请提供出路。提前致谢。

这不是 GeoJSON。 features.geometry 字段应该是 GeoJSON,但它也不是 GeoJSON,因为您有一个空值的额外键:

"geometry" : {
        "coordinates" : [
            86.74957, 
            21.93157
        ], 
        "type" : "Point",
        "crs" : null,        // not valid
        "bbox" : [           
            86.74957,
            21.93157,      
            86.74957, 
            21.93157
        ]
    }

如果您从 GeoJSON 字段中删除额外的键,地理索引的创建不会出错。

您可以使用 GeoJSON Lint 非常可靠地验证 GeoJSON。

我删除了包含空数据的字段。在我的例子中它是 "crs" : null,。 并且索引创建成功。