0

I have a collection called "stops" that stores some coordinate info. I used MongoDB 2dsphere index for searching places. For example, I want to query all the stops around a certain position using 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:

{ "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

{"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.

5
  • Instead of runCommand start trying with aggregate, like: db.stops.aggregate([{ $geoNear: { near: { type: "Point", coordinates: [-123.115115, 49.209659] }, spherical: true, minDistance: 0, maxDistance: 200, } }]) Commented Sep 21, 2016 at 20:31
  • @DiegoZoracKy Thanks! I've tried that. And returning:"errmsg" : "$geoNear requires a 'distanceField' option as a String", I think the value after geoNear has to be a collection name Commented Sep 21, 2016 at 20:35
  • 1
    Yes, in that case it needs the distanceField. Take a look here: docs.mongodb.com/manual/reference/operator/aggregation/geoNear Commented Sep 21, 2016 at 20:37
  • @DiegoZoracKy Thanks! That works! Commented Sep 21, 2016 at 20:44
  • Good! I will turn this into a answer, so we can close it. Commented Sep 21, 2016 at 20:47

1 Answer 1

1

Use aggregate instead of 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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.