MongoDB geoNear 不适用于某些坐标但适用于其他坐标
MongoDB geoNear not working for some coordinates but working for others
我正在使用 MongoDB 和 mongoDB 的 Node.js 驱动程序开发 API。其中一条路线接受坐标并调用聚合管道,该管道 returns 结果按接近度和许多其他因素排序。管道第一步的代码是:
{$geoNear:{
near:{
type: "Point",
coordinates: [lat, long]
},
spherical:true,
distanceField:"distance"
}},
这一切都很好,直到突然之间,它不是。现在它适用于某些坐标,而对于其他坐标则给出错误 MongoError: invalid argument in geo near query: type
。它适用于 {lat:0, long:0}
,适用于 {lat:40.7411, long:-73.9897}
,适用于 {lat:46.68758191106798, long:2.8106467331441767}
,但适用于 {lat:37.785834, long:-122.406417}
和 {lat:47.643628614308675, long:-122.34560056016635}
。我能找到的唯一模式是它讨厌西海岸的坐标。我已经验证 2d 球体索引仍然存在于数据中。我试过将 spherical 设置为 false。我什至尝试删除所有数据,即使是空集合,仍然会出现相同的行为。我真的很困惑为什么 geonear 的论点对某些点有效而不对其他点有效。
编辑:我现在已经注释掉了除以下代码之外的所有内容,并且我仍然得到相同的行为,其中一些坐标有效而其他坐标给出 MongoError: invalid argument in geo near query: type
router.post("/", asyncHandler(async (req, res, next) => {
const {lat, long} = req.body;
const errors = [];
if(typeof(lat) !== "number" || Math.abs(lat) > 90) errors.push("Lat is invalid");
if(typeof(long) !== "number" || Math.abs(long) > 180) errors.push("Long is invalid");
if(errors.length) return next(errors);
const feed = await req.app.locals.db.collection("posts").aggregate(
[
{$geoNear:{
near:{
type: "Point",
coordinates: [lat, long]
},
spherical:true,
distanceField:"distance"
}},
]
);
const feedArr = await feed.toArray();
res.json(feedArr);
}));
If specifying latitude and longitude coordinates, list the longitude first and then latitude
你做到了coordinates: [lat, long]
我正在使用 MongoDB 和 mongoDB 的 Node.js 驱动程序开发 API。其中一条路线接受坐标并调用聚合管道,该管道 returns 结果按接近度和许多其他因素排序。管道第一步的代码是:
{$geoNear:{
near:{
type: "Point",
coordinates: [lat, long]
},
spherical:true,
distanceField:"distance"
}},
这一切都很好,直到突然之间,它不是。现在它适用于某些坐标,而对于其他坐标则给出错误 MongoError: invalid argument in geo near query: type
。它适用于 {lat:0, long:0}
,适用于 {lat:40.7411, long:-73.9897}
,适用于 {lat:46.68758191106798, long:2.8106467331441767}
,但适用于 {lat:37.785834, long:-122.406417}
和 {lat:47.643628614308675, long:-122.34560056016635}
。我能找到的唯一模式是它讨厌西海岸的坐标。我已经验证 2d 球体索引仍然存在于数据中。我试过将 spherical 设置为 false。我什至尝试删除所有数据,即使是空集合,仍然会出现相同的行为。我真的很困惑为什么 geonear 的论点对某些点有效而不对其他点有效。
编辑:我现在已经注释掉了除以下代码之外的所有内容,并且我仍然得到相同的行为,其中一些坐标有效而其他坐标给出 MongoError: invalid argument in geo near query: type
router.post("/", asyncHandler(async (req, res, next) => {
const {lat, long} = req.body;
const errors = [];
if(typeof(lat) !== "number" || Math.abs(lat) > 90) errors.push("Lat is invalid");
if(typeof(long) !== "number" || Math.abs(long) > 180) errors.push("Long is invalid");
if(errors.length) return next(errors);
const feed = await req.app.locals.db.collection("posts").aggregate(
[
{$geoNear:{
near:{
type: "Point",
coordinates: [lat, long]
},
spherical:true,
distanceField:"distance"
}},
]
);
const feedArr = await feed.toArray();
res.json(feedArr);
}));
If specifying latitude and longitude coordinates, list the longitude first and then latitude
你做到了coordinates: [lat, long]