【发布时间】: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