【问题标题】:mongo geonear aggregate framework errormongo geonear 聚合框架错误
【发布时间】:2015-12-21 04:42:00
【问题描述】:

我正在尝试让以下查询与我的地址模型一起使用

http://docs.mongodb.org/manual/reference/operator/aggregation/geoNear/

我的模型是这样的

  "_id" : "55e107b057ea3d17f8adfab1",
  "DateCreated" : ISODate("2015-08-29T01:15:28.505Z"),
  "DateModified" : ISODate("2015-08-29T01:15:28.505Z"),
  "UserModified" : "geocoder",
  "IsDeleted" : false,
  "ExternalId" : "",
  "ExternalNumber" : "",
  "HashCode" : "",
  "TenantId" : "global",
  "Address" : {
    "Street" : "13305 104TH ST",
    "City" : "PLEASANT PRAIRIE",
    "StateProvince" : "WI",
    "PostCode" : "",
    "County" : "us",
    "Country" : "United States of America",
    "CountryCode" : "Kenosha County",
    "Gps" : {
      "bbox" : [-180.0, -90.0, 180.0, 90.0],
      "geometry" : {
        "type" : "Point",
        "coordinates" : [-87.917057, 42.524231]
      },
      "type" : "Feature"
   }
 }

我尝试运行这个查询

db.AddressInformations.aggregate([
    {
     $geoNear: {
     near: { type: "Point", "Address.Gps.geometry": [ -73.99279 , 40.719296 ] },
        distanceField: "dist.calculated",
        maxDistance: 2,
        includeLocs: "dist.location",
        num: 5,
        spherical: true
     }
   }
])

然后我得到这个错误

assert: command failed: {
    "errmsg" : "exception: geoNear command failed: { ok: 0.0, errmsg: \"no geo indices for geoNear\" }",
    "code" : 16604,
    "ok" : 0

【问题讨论】:

  • 您是否在"Address.Gps.geometry" 上创建了地理空间索引?
  • 是 db.AddressInformations.createIndex({"Address.Gps.geometry" : "2dsphere"});

标签: mongodb mongodb-query aggregation-framework


【解决方案1】:

您正在尝试将路径传递给“near”参数中的字段。这应该是坐标对或 GeoJSON 定义。它不像字段查询,因为 $geoNear 只希望存在 一个 索引,它只会选择那个:

db.AddressInformations.aggregate([
    { "$geoNear": {
        "near": { 
            "type": "Point", 
            "coordinates": [ -73.99279 , 40.719296 ]
        },
        "distanceField": "dist.calculated",
        "maxDistance": 2,
        "includeLocs": "dist.location",
        "num": 5,
        "spherical": true
    }}
])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-10
    • 2015-04-25
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 2013-04-29
    • 2023-03-21
    • 2018-08-25
    相关资源
    最近更新 更多