【问题标题】:Error while building geo index using mongodb and nodejs使用 mongodb 和 nodejs 构建地理索引时出错
【发布时间】:2014-07-14 18:02:06
【问题描述】:

我正在尝试建立地理索引

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

【问题讨论】:

    标签: node.js mongodb indexing


    【解决方案1】:

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

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

    2.1.2.点

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

    您可以在这里测试您的 Geojson 数据:

    http://geojsonlint.com/

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

    【讨论】:

    • 打败我。是的,您可以将无效的 GeoJSON 插入 MongoDB,因为它仍然是有效的 JSON,并且只有在您尝试创建空间索引或执行空间查询时才会变得明显。
    • 感谢约翰 B 和约翰 P
    猜你喜欢
    • 1970-01-01
    • 2012-11-07
    • 2018-01-30
    • 2018-01-25
    • 2015-05-31
    • 2012-06-24
    • 1970-01-01
    • 2023-03-16
    • 2016-10-02
    相关资源
    最近更新 更多