node.js - $geoNear 仅在我给出 maxDistance = 100 * 1000 时给出结果;
问题描述
我想要附近的律师名单,我使用了 MongoDB 聚合,但只有当我通过 maxDistance 大于 100 公里时它才会给出结果。如果我想设置 maxDistance = 10 * 1000; 那么它没有给出任何结果,但数据库中有可用的数据。
这是模型:
const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const lawyerSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
location: {
type: { type: String, default: 'Point' },
coordinates: { type: [Number], default: [0, 0] }
}
})
lawyerSchema.index({ "location": "2dsphere" });
const Lawyer = mongoose.model('Lawyer', lawyerSchema)
module.exports = Lawyer
这是路线:
//route for find nearby lawyer
router.get("/findLawyer", (req, res) => {
const long = parseInt(req.query.long);
const lat = parseInt(req.query.lat);
Lawyer.aggregate(
[
{
$geoNear: {
near: {
type: "Point",
coordinates: [long, lat],
},
distanceField: "dist",
minDistance: 10 * 1000,
maxDistance: 10 * 1000,
distanceMultiplier: 6371,
spherical: true,
key: "location",
},
},
],
(err, results) => {
if (err) {
res.status(400).json({ error: err });
} else {
res.status(200).json({ result: results });
}
}
);
});
这是数据库内的记录:
这是我的邮递员:
如果我犯了任何错误,请纠正我。谢谢
解决方案
推荐阅读
- email - 我正在尝试找到一种方法来根据他们的电子邮件地址从我的系统中识别已删除的用户帐户,而不会违反 gdpr / 隐私
- javascript - 如何根据包含特定字符的对象字符串拆分数组?
- html - Devtool 在检查时为空 - 无法使用 FindElementById
- python - 如何在python中提取数字和下划线
- javascript - 如何获取字幕当前显示的文本
- kubernetes - 如何使有问题的 pod 脱机以进行故障排除
- python - 合并 id 重复的字典列表 - python3
- javascript - 在 insertAdjacentHTML 之后更改类名
- python - python pandas平均连续值数
- html - 缩放 SVG 以显示其自身