mongo geonear聚合框架错误

时间:2015-09-23 18:21:36

标签: mongodb mongodb-query aggregation-framework

我正在尝试使用我的地址模型进行以下查询

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

1 个答案:

答案 0 :(得分:2)

您正尝试将路径传递到“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
    }}
])