我有一个名为"停止"存储一些坐标信息。我使用MongoDB 2dsphere索引来搜索地点。例如,我想使用db.runCommand
查询某个位置周围的所有站点:
db.runCommand({
geoNear: "stops",
near: { type: "Point" , coordinates: [ -123.115115, 49.209659 ] },
spherical: true,
minDistance: 0,
maxDistance: 200,
})
结果是一个json数组:
{
"waitedMS" : NumberLong(0),
"results" : [
{
"dis" : 79.0147493688595,
"obj" : {
"_id" : ObjectId("57e2349b9d0263463a3e93aa"),
"zone_id" : "ZN 99",
"coordinate" : [
-123.116116,
49.209383
],
"stop_id" : 11252,
"stop_code" : 61338,
"stop_url" : "",
"stop_desc" : "MARINE DRIVE STATION LOOP @ BAY 2",
"stop_name" : "MARINE DRIVE STN BAY 2",
"location_type" : 0,
"parent_station" : ""
}
},
{
"dis" : 140.73823410181,
"obj" : {
"_id" : ObjectId("57e2349b9d0263463a3eaec9"),
"zone_id" : "ZN 1",
"coordinate" : [
-123.117038,
49.209801
],
"stop_id" : 11286,
"stop_code" : "",
"stop_url" : "",
"stop_desc" : "SKYTRAIN @ MARINE DRIVE STN",
"stop_name" : "MARINE DRIVE STATION",
"location_type" : 0,
"parent_station" : ""
}
}
],
"stats" : {
"nscanned" : 14,
"objectsLoaded" : 2,
"avgDistance" : 123.782109714949,
"maxDistance" : 140.73823410181,
"time" : 1
},
"ok" : 1.0
}
我想知道我们可以做一个嵌套的runCommand来进一步过滤结果就像
{"results" : [
{
"dis" : 79.0147493688595,
"obj" : {
"coordinate" : [
-123.116116,
49.209383
],
"stop_id" : 11252,
"stop_code" : 61338,
"stop_desc" : "MARINE DRIVE STATION LOOP @ BAY 2",
"stop_name" : "MARINE DRIVE STN BAY 2",
}
},
{
"dis" : 140.73823410181,
"obj" : {
"coordinate" : [
-123.117038,
49.209801
],
"stop_id" : 11286,
"stop_code" : "",
"stop_desc" : "SKYTRAIN @ MARINE DRIVE STN",
"stop_name" : "MARINE DRIVE STATION",
}
}
]
}
原始的json响应中有很多无用的信息。
答案 0 :(得分:1)
使用aggregate
代替runCommand
。这就像是:
db.stops.aggregate([{
$geoNear: {
near: { type: "Point", coordinates: [-123.115115, 49.209659] },
spherical: true,
minDistance: 0,
maxDistance: 200,
distanceField: 'someDistanceFieldProperty'
}
}]);
查看文档,让您更进一步:https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear/