简体   繁体   English

如何使用猫鼬从mongodb中按2个地理点查找?

[英]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. 我正在研究一个项目,使用NodeJS(mongoose驱动程序)将MongoDB作为数据库,并将Angular 6放在前面。 We need realise on back some feature to search some data by 2 geoPoints. 我们需要实现一些功能,以便按2个geoPoints搜索某些数据。 Actually i can't find any information how can i store 2 points and find by this points in mongo. 实际上,我找不到任何信息,该如何在mongo中存储2个点并以此点查找。

I find some info on mongoose documentation about GeoJSON querying with $near but it search only by one point. 我在猫鼬文档中找到了一些有关使用$ near查询GeoJSON的信息,但它只搜索一点。

I believe, you need way more advanced spatial functionality than MongoDB might offer. 我相信,您需要比MongoDB可能提供的更高级的空间功能。 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. 如果您正在寻找一种方法来查找到2个给定点的总距离最小的点(以后可能需要其他空间数据计算),则建议使用PostGIS(PostgreSQL空间附加组件)或一些javascript MongoDB顶部的库来发布处理GeoJSON数据。

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

                    });

                }
            )

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

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