Chadit
Chadit

Reputation: 965

mongo geonear aggregate framework error

I am trying to get the following query working with my address model

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

My model looks like this

  "_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"
   }
 }

I try and run this query

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

I then get this error

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

Upvotes: 1

Views: 3349

Answers (1)

Blakes Seven
Blakes Seven

Reputation: 50406

You are trying to pass the path to the field in the "near" argument. This should either be a coordinate pair or a GeoJSON definition instead. It's not like a field query as $geoNear expects only one index to be present and it will select that one only:

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

Upvotes: 2

Related Questions