使用 mongodb 查找附近位置的对象 |所有附近的位置 A 到所有附近的位置 B
Find objects of near locations using mongodb | all nearby location A to all nearby location B
嗨MongoDB专家
我正在尝试使用 MongoDB 各种位置特征($near、$geoNear 等)来获得一些查询结果。
我有这个 geoJSON 类型的猫鼬模型。
const geoSchema = new Schema({
type: {
type: String,
default: 'Point',
},
coordinates: {
type: [Number],
},
});
const pickupSchema = new Schema({
geo_location_from: geoSchema,
geo_location_to: geoSchema,
});
pickupSchema.index({ geo_location_from: '2dsphere' });
pickupSchema.index({ geo_location_to: '2dsphere' });
我想要实现的是在活动地点附近。
我有从 A 到 B 的主要接送事件,如图所示,我有所有位置的纬度和经度。现在,我正在尝试从数据库中查询所有这些事件对象,其中事件 geo_location_from 靠近位置 A(例如:A1、A2、A3)并且 geo_location_to 靠近位置 B(B1、B2) ).
这是我做的,这是不对的。我不是 100% 确定。
Pickup.find(
{
$and: [{
geo_location_from: {
$near: {
$maxDistance: 1000,
$geometry: {
type: 'Point',
coordinates: [args.longitude_from, args.latitude_from],
},
},
},
}, {
geo_location_to: {
$near: {
$maxDistance: 1000,
$geometry: {
type: 'Point',
coordinates: [args.longitude_to, args.latitude_to],
},
},
},
}],
},
)
我的一些尝试最终给出了各种错误。
像 太多 geoNear 表达式 等等。
大家有什么好的解决办法吗?
所以我一直在想一个聪明的方法来完成你所要求的一切,但我认为只用 MongoDb 很难(如果不是不可能)做到这一点。
一个完美的解决方案是恕我直言,将 turf.js 与 MongoDb.
一起使用
我想出了一个我认为可能是你正在寻找的方法。
为了得到所有同时靠近space中两个点的地方,我们需要定义一个多边形(一个区域)来寻找这些地方。鉴于大多数时候你只有 2 个点,多边形必须类似于圆形。
GeoJson 无法做到这一点,所以 turf.js 进来了。你可以这样做:
// create the points
let point1 = turf.point([-90.548630, 14.616599]);
let point2 = turf.point([-88.548630, 14.616599])
// we get the midpoint that we'll use as the center of our circle
const midPoint = turf.midpoint(point1, point2)
// we create a polygon that we'll use as our area
const options = {steps: 100, units: 'kilometers'}; // options for the circle
const circle = turf.circle(midPoint, options)
// a possible place near both locations
let point3 = turf.point([-91.43897, 14.56784])
// then you can get if a point is inside the circle or not
const inside = turf.booleanWithin(point3, circle)
这只是一个想法。您可以自定义圆的半径以获得更远或更近的地方。然后,您当然可以按照靠近中心的位置对您在圆圈内找到的位置进行排序(这可以在 MongoDb 中轻松完成)。
我建议您仔细查看 turf.js 和 MongoDb 文档以获得关于如何使它们无缝协同工作的更清晰的想法(并且可能找到比我的更好、更简单的解决方案)。
嗨MongoDB专家
我正在尝试使用 MongoDB 各种位置特征($near、$geoNear 等)来获得一些查询结果。
我有这个 geoJSON 类型的猫鼬模型。
const geoSchema = new Schema({
type: {
type: String,
default: 'Point',
},
coordinates: {
type: [Number],
},
});
const pickupSchema = new Schema({
geo_location_from: geoSchema,
geo_location_to: geoSchema,
});
pickupSchema.index({ geo_location_from: '2dsphere' });
pickupSchema.index({ geo_location_to: '2dsphere' });
我想要实现的是在活动地点附近。
我有从 A 到 B 的主要接送事件,如图所示,我有所有位置的纬度和经度。现在,我正在尝试从数据库中查询所有这些事件对象,其中事件 geo_location_from 靠近位置 A(例如:A1、A2、A3)并且 geo_location_to 靠近位置 B(B1、B2) ).
这是我做的,这是不对的。我不是 100% 确定。
Pickup.find(
{
$and: [{
geo_location_from: {
$near: {
$maxDistance: 1000,
$geometry: {
type: 'Point',
coordinates: [args.longitude_from, args.latitude_from],
},
},
},
}, {
geo_location_to: {
$near: {
$maxDistance: 1000,
$geometry: {
type: 'Point',
coordinates: [args.longitude_to, args.latitude_to],
},
},
},
}],
},
)
我的一些尝试最终给出了各种错误。 像 太多 geoNear 表达式 等等。
大家有什么好的解决办法吗?
所以我一直在想一个聪明的方法来完成你所要求的一切,但我认为只用 MongoDb 很难(如果不是不可能)做到这一点。
一个完美的解决方案是恕我直言,将 turf.js 与 MongoDb.
一起使用
我想出了一个我认为可能是你正在寻找的方法。
为了得到所有同时靠近space中两个点的地方,我们需要定义一个多边形(一个区域)来寻找这些地方。鉴于大多数时候你只有 2 个点,多边形必须类似于圆形。
GeoJson 无法做到这一点,所以 turf.js 进来了。你可以这样做:
// create the points
let point1 = turf.point([-90.548630, 14.616599]);
let point2 = turf.point([-88.548630, 14.616599])
// we get the midpoint that we'll use as the center of our circle
const midPoint = turf.midpoint(point1, point2)
// we create a polygon that we'll use as our area
const options = {steps: 100, units: 'kilometers'}; // options for the circle
const circle = turf.circle(midPoint, options)
// a possible place near both locations
let point3 = turf.point([-91.43897, 14.56784])
// then you can get if a point is inside the circle or not
const inside = turf.booleanWithin(point3, circle)
这只是一个想法。您可以自定义圆的半径以获得更远或更近的地方。然后,您当然可以按照靠近中心的位置对您在圆圈内找到的位置进行排序(这可以在 MongoDb 中轻松完成)。
我建议您仔细查看 turf.js 和 MongoDb 文档以获得关于如何使它们无缝协同工作的更清晰的想法(并且可能找到比我的更好、更简单的解决方案)。