Search code examples
mongodbspatial-index2dsphere

MongoDB 2dsphere spatial index creation error


In MongoDB I have created a database named 'GIS' and a collection named 'UTILITIES' and within the collection I have inserted a BSON document as below.

{ 
"_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
]
}

The document is actually a GeoJSON. I tried creating an index as

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

which 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
}

I have checked the longitude and latitude order which is ok.

Kindly provide a wayout. Thanks in advance.


Solution

  • I removed the fields containing null data. In my case its "crs" : null,. And the index created successfully.