简体   繁体   English

MongoDB嵌套RunCommand

[英]MongoDB Nested RunCommand

I have a collection called "stops" that stores some coordinate info. 我有一个名为“ stops”的集合,其中存储了一些坐标信息。 I used MongoDB 2dsphere index for searching places. 我使用MongoDB 2dsphere索引搜索位置。 For example, I want to query all the stops around a certain position using db.runCommand : 例如,我想使用db.runCommand查询某个位置周围的所有站点:

db.runCommand({ geoNear: "stops", near: { type: "Point" , coordinates: [ -123.115115, 49.209659 ] }, spherical: true, minDistance: 0, maxDistance: 200, })

The result is an json array: 结果是一个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 }

I am wondering can we do a nested runCommand to further filter the result to be like 我想知道我们能否做一个嵌套的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", } } ] }

There is a lot of useless info in original json response. 原始json响应中有很多无用的信息。

Use aggregate instead of runCommand . 使用aggregate而不是runCommand It would be like: 就像:

db.stops.aggregate([{
    $geoNear: {
        near: { type: "Point", coordinates: [-123.115115, 49.209659] },
        spherical: true,
        minDistance: 0,
        maxDistance: 200,
        distanceField: 'someDistanceFieldProperty'
    }
}]);

Take a look at the docs for you to go further: https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear/ 请看一下文档以进一步了解: https : //docs.mongodb.com/manual/reference/operator/aggregation/geoNear/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM