简体   繁体   中英

MongoDB Geospatial, project matching multi points

Via MongoDB $geoNear command with Aggregate, I'm unable to generate locations that are within the queried maxDistance field.

        array(
            '$geoNear'          => array(
                'near'             => array(
                    'type'            => "MultiPoint",
                    'coordinates'     => [ 
                        $city["geo"]["coordinates"][0], 
                        $city["geo"]["coordinates"][1] 
                    ],
                ),
                "distanceField"    => "distance",
                "maxDistance"      => 1*1609.34,
                "includeLocs"      => "locations",
                "spherical"        => true,
                "query" => array(
                    "status" => true
                ),
                "limit" => 500
            ),
        ),

The documentation states to use includeLocs " specifies the output field that identifies the location used to calculate the distance. This option is useful when a location field contains multiple locations .", however the includeLocs field is including all multipoint locations.

Does mongodb have the ability to project only the matching multipoints and not the whole set stored?

I was able to solve this:

  1. After the $geoNear pipeline, add an $unwind stage to unwind the "includeLocs" array ie {$unwind: '$locations.coordinates'} (Make sure you unwind the coordinates).
  2. Add another stage for '$match' with the $geoWithin geospatial operator with the field marked specifically to the unwinded coordinates field ie '$locations.coordinates'.
  3. Finally add a '$group' stage to join everything back to the _id field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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