pkpk
pkpk

Reputation: 832

Error while building geo index using mongodb and nodejs

I'm trying to build a geo index

db.collection('servicereg').aggregate([

{
    $geoNear : {
        near : [ -121.9181731, 37.4544319 ],
        distanceField : "dist",
        maxDistance : 500000,
        // query: { "servdescription": { $regex: "need" } },
        // includeLocs: "dist.location",
        // num: 1,
        spherical : true

    }
} ], function(err, result) {
    if (err)
        throw err;
    console.log("agg result", result);
    console.log("error", err);
});

Here is the index creation command

db.collection('servicereg').ensureIndex( {"location.longlat" : "2dsphere"},  
function(err, items) {

console.log(err);
console.log(items);
});

Here are couple of documents

    {
  "_id" : ObjectId("53c23d668881199c1474b070"),
  "servicename" : "service1",
  "servdescription" : "battery change",
  "email" : "[email protected]",
  "location" : {
    "country" : "United States",
    "city" : "Beaverton",
    "state" : "Oregon",
    "stateCode" : "OR",
    "zipcode" : "97006",
    "streetName" : "Southwest Millikan Way",
    "streetNumber" : "15110",
    "countryCode" : "US",
    "longlat" : {
      "type" : "point",
      "coordinates" : [-122.8325687, 45.495655]
    }
  },
  "price" : null,
  "expiry" : null,
  "category" : null,
  "dateofservice" : null
}

/* 6 */
{
  "_id" : ObjectId("53c23d998881199c1474b071"),
  "servicename" : "se23",
  "servdescription" : "need aspirin",
  "email" : "[email protected]",
  "location" : {
    "country" : "United States",
    "city" : "Milpitas",
    "state" : "California",
    "stateCode" : "CA",
    "zipcode" : "95035",
    "streetName" : "Dixon Landing Road",
    "streetNumber" : "440",
    "countryCode" : "US",
    "longlat" : {
      "type" : "point",
      "coordinates" : [-121.9181734, 37.4544319]
    }
  },
  "price" : null,
  "expiry" : null,
  "category" : null,
  "dateofservice" : null
 }

While building the index, I get the following error

        {
          [
            MongoError: Can't extract geo keys from object, malformed geometry?:    
                 { 
                   _id: ObjectId('53c23ba68881199c1474b06f'), 
                   servicename: "service11", 
                   servdescription: "oil change", 
                   email: "[email protected]", 
                   location: {  
                        country: "United States", 
                        city: "Redwood City", 
                        state: "California", 
                        stateCode: "CA", 
                        zipcode: "94065", 
                        streetName: "Island Drive", 
                        streetNumber: "1100", 
                        countryCode: "US",            
                        longlat: { type: "point", coordinates: [ -122.2562282, 37.5366173 ] } 
                        },
                   price: null, expiry: null, category: null, dateofservice: null 
                 }
          ] name: 'MongoError', connectionId: 229, err: 'Can\'t extract geo keys from object, malformed geometry?: 
             { 
                 _id: ObjectId(\'53c23ba68881199c1474b06f\'), 
                 servicename: "service11",
                 servdescription: "oil change", 
                 email: "[email protected]", 
                 location: { 
                      country: "United States", 
                      city: "Redwood City", 
                      state: "California", 
                      stateCode: "CA", 
                      zipcode: "94065", 
                      streetName: "Island Drive", 
                      streetNumber: "1100", 
                      countryCode: "US", 
                      longlat: { type: "point", coordinates: [ -122.2562282, 37.5366173 ] } }, 
                  price: null, expiry: null, category: null, dateofservice: null 
                }',
              code: 16755,
              n: 0,
              ok: 1
           }

Anybody has any idea, what is wrong here ?

Thanks in advance

PK

Upvotes: 0

Views: 187

Answers (1)

John Petrone
John Petrone

Reputation: 27515

You have an incorrect type specification for your data. The correct type is "Point" not "point":

http://geojson.org/geojson-spec.html

2.1.2. Point

For type "Point", the "coordinates" member must be a single position.

You can test your Geojson data here:

http://geojsonlint.com/

Note that when test it says that there is no such type as "point".

Upvotes: 1

Related Questions