3

While trying to setup to use the Geospatial Index on MongoDB, I run into the error message that the location array is not in the correct format.

This is my collection "test".

{
    "_id" : ObjectId("4f037ac176d6fdab5b00000a"),
    "CorporateId" : "XYZ12345",
    "Places" : [
           {

                   "Location" : {
                           "Longitude" : "50.0",
                           "Latitude" : "50.0"
                   },
                   "ValidFrom" : "2011-11-01 00:00:00",
                   "ValidTo" : "2021-12-31 00:00:00",
                   "itemCount" : "1"
           }
    ]
}

Once I run this code.

db.test.ensureIndex({"Places.Location": "2d"});

I get this error message

location object expected, location array not in correct format

My assumption is that the Long/Lat needs to be a number. Currently it is an object.

typeof db.test.Places.Location.Longitude -> Object
typeof db.test.Places.Location -> Object

My problem is that since I am still quite new to MongoDB, I don't really know how to approach this problem in the best way.

2
  • What client application are you using? Commented Jan 4, 2012 at 3:42
  • Yes, it needs to be a number. Commented Jan 4, 2012 at 3:44

2 Answers 2

10

Mongodb expects numbers for the coordinates while you passed in a string.

"Location" : {
                       "Longitude" : 50.0, // <<<<<<<<<<<<<< use numbers instead
                       "Latitude" : 50.0
               },

see http://www.mongodb.org/display/DOCS/Geospatial+Indexing for details.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the quick help. Is there a way I can force the collection to insert this as a number?
you may use parseFloat() to coerce the coordinates into numbers before insertion.
My problem is that the insertion routine basically just saves a serialized json array into the DB. This would mean that I have to parse the JSON String before insertion. Please excuse my lack of knowledge, is that the recommended approach ?
Sorry, I failed to figure out another approach.
Yes, you'd need to parse the JSON (or fix it upstream). There is no way to use any kind of schema to enforce this in Mongo.
0

The problem has been fixed by converting the Location parameters into a float type like this.

$var = $JSonString['Places'];
 $test=count($var);

 $i=0;
 for ( $i=0; $i<$test;$i++){
       $lon = (float)$JSonString['Places'][$i]['Location']['Longitude'];
       $lat = (float)$JSonString['Places'][$i]['Location']['Latitude'];
       $JSonString['Places'][$i]['Location']['Longitude'] =$lon ;
       $JSonString['Places'][$i]['Location']['Latitude'] =$lat ;
       //error_log($lon . "->".gettype($JSonString['Places'][$i]['Location']['Latitude']), 3 , "/var/tmp/my-errors.log");
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.