0
votes

During a unit test on MongoDB geo data query, I get the following error message:

can't find any special indices: 2d (needs index), 2dsphere (needs index), for: { address.location: { $nearSphere: { x: -120.0, y: 47.0 }, $maxDistance: 0.01567855942887398 }, status: "INIT" }; nested exception is com.mongodb.MongoException: can't find any special indices: 2d (needs index), 2dsphere (needs index), for: { address.location: { $nearSphere: { x: -120.0, y: 47.0 }, $maxDistance: 0.01567855942887398 }, status: "INIT" }

After running the createIndex command, I however, get the following errors:

db.store.createIndex({"address.location":"2d"}) location object expected, location array not in correct format db.store.createIndex({"address.location":"2dsphere"}) Can't extract geo keys from object, malformed geometry?:{ type: "Point", coordinates: [ 47.399465, -122.2950165 ] }

The structure of the document data is the followings:

{
 ....
       "address" : {
           "street" : "Street One",
           "city" : "One City",
           "province" : "aProvince",
           "country" : "aCountry",
           "postalCode" : "91202",
           "location" : {
                   "type" : "Point",
                   "coordinates" : [
                           47.5891279,
                           -122.0446183
                   ]
           }
   }

What I do it wrong?

1

1 Answers

5
votes

It looks like your "longitude" and "latitude" are in the reverse order, and the error should be telling you that a well ( as least in 3.x series it does ). You have "latitude" and "longitude", so reverse it:

{
   "address" : {
       "street" : "Street One",
       "city" : "One City",
       "province" : "aProvince",
       "country" : "aCountry",
       "postalCode" : "91202",
       "location" : {
           "type" : "Point",
           "coordinates" : [
               -122.0446183,
               47.5891279
           ]
       }
    }
}

And the index creates without error:

db.store.ensureIndex({ "address.location": "2dsphere" })

That is for a "2dsphere" index. A "2d" index cannot be created on a GeoJSON object, so you can only use the "coordinates" directly:

db.store.ensureIndex({ "address.location.coordinates": "2d" })

It's "planar" so the GeoJSON format does not apply.