使用mongodb和nodejs构建地理索引时出错

时间:2014-07-14 18:02:07

标签: node.js mongodb indexing

我正在尝试构建地理索引

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);
});

这是索引创建命令

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

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

以下是几个文件

    {
  "_id" : ObjectId("53c23d668881199c1474b070"),
  "servicename" : "service1",
  "servdescription" : "battery change",
  "email" : "testuser110@gmail.com",
  "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" : "testuser110@gmail.com",
  "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
 }

构建索引时,出现以下错误

        {
          [
            MongoError: Can't extract geo keys from object, malformed geometry?:    
                 { 
                   _id: ObjectId('53c23ba68881199c1474b06f'), 
                   servicename: "service11", 
                   servdescription: "oil change", 
                   email: "testuser110@gmail.com", 
                   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: "testuser110@gmail.com", 
                 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
           }

任何人都有任何想法,这里有什么问题?

提前致谢

PK

1 个答案:

答案 0 :(得分:1)

您的数据类型规范不正确。正确的类型是“点”而不是“点”:

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

  

<强> 2.1.2。点

     

对于“Point”类型,“coordinates”成员必须是单个位置。

您可以在此处测试您的Geojson数据:

http://geojsonlint.com/

请注意,在测试时它表示没有“点”这样的类型。