简体   繁体   中英

How to find by 2 geopoints from mongodb using mongoose?

I'm researching in one project with NodeJS(mongoose driver) back with MongoDB as database and Angular 6 on front. We need realise on back some feature to search some data by 2 geoPoints. Actually i can't find any information how can i store 2 points and find by this points in mongo.

I find some info on mongoose documentation about GeoJSON querying with $near but it search only by one point.

I believe, you need way more advanced spatial functionality than MongoDB might offer. If you're looking for a way to find some point that has minimal total distance to 2 given points (and you may later require other spatial data calculations), I'd suggest to use PostGIS (PostgreSQL spatial add-on) or some javascript library on top of MongoDB to post process GeoJSON data.

Many thanks! it's look like i should look up documentation more deeper) code below is the way that helped me with my problem:

Way.aggregate(
                [{
                    $match: {
                        'origin.coordinates': {
                            $geoWithin: { $centerSphere: [ [ origin.lat, origin.lng ], 0.2/6378.1 ] }
                        },
                        'destination.coordinates': {
                            $geoWithin: { $centerSphere: [ [ destination.lat, destination.lng ], 0.2/6378.1 ] }
                        }
                    }
                }], function(error, results){

                    Way.populate(results, {path: 'user'}, function(err, ready){

                    });

                }
            )

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