首页 > 解决方案 > 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.

标签: javascriptnode.jsmongodbgeojson

解决方案


非常感谢!看起来我应该更深入地查找文档)下面的代码是帮助我解决问题的方法:

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

                    });

                }
            )

推荐阅读