我在一个 typescript 服务器框架 Nest 中使用 Mongo 数据库和 mongoose。
我有 2 个 mongo 集合,其中一个包含 20,000 个用户位置。另一个集合包含从 Google Places API 收集的 10,000 个兴趣点。
我现在想找到收集的位置与这些位置(包含 lat 和 lng GeoJSON 点)之间的交点。
换句话说,我正在寻找与这些 POI 的用户相关的位置。
目前,我有一个异步方法,可以使用 nearSphere 运算符查找某个点附近的所有位置。
然后我认为下一步将是遍历 mongo 集合中的每个位置(其中 10,000 个)并在每个位置上运行此方法。这样一来,当该特定位置被捕获时,我将获得“附近”哪些 POI 的列表。
有没有更好的方法来做到这一点?关于性能,我相信这种方式会很困难。我找不到可以让我比较两组位置的 mongo 地理空间查询。
async findAllNearPlace(coords): Promise<Location[]> {
return await this.locationModel.find(
{
location:
{ $nearSphere:
{
$geometry: { type: "Point", coordinates: coords },
$minDistance: 0,
$maxDistance: 100
}
}
}
);
}
每个 POI - 检查位置:
async findUsersInProximity(places): Promise<Location[]> {
const locations = [];
let i = places.length - 1;
while (i > 0) {
await this.findAllNearPlace(
places[i].location.coordinates
).then(intersectingLocations => {
locations.push(...intersectingLocations);
i--;
});
}
return await locations;
}
正如预期的那样,它的性能很差,需要几分钟。
最佳答案
您可能可以做的是使用查找来制作聚合函数,我没有测试它,我不确定它是否具有更好的性能,但是您可以执行类似于以下的操作:
let pipeline = [
{
$geoNear: {
includeLocs: "location",
distanceField: "distance",
near: { type: 'Point', coordinates: "$$point" },
maxDistance: 20,
spherical: true
}
}
];
UsersModel.aggregate([{
$lookup : {
from : 'locations',
let : {point : '$address'}, //whatever the field for the coordinates is
pipeline ,
as : 'places'
}
}])
https://stackoverflow.com/questions/56039138/
相关文章:
mongodb - 如何验证由 mongodump 操作产生的文件的完整性?
javascript - 不确定如何在 Express/MongoDB 应用程序中处理数据访问对象/
mongodb - .NET 驱动程序与 LINQ : NotSupportedException:
mongodb - Mongo中的SocketException
java - 在等待与 WritableServerSelector 匹配的服务器时超时 30000
mongodb - Mongoid 没有服务器可用匹配首选项
mongodb - 玩 2.3.5 和 ReactiveMongo : MongoError ['N
mongodb - 为什么 MongoDB 在负载测试期间没有响应?