问题描述
我想要附近的律师名单, 我使用了 MongoDB 聚合,但只有当我通过 maxdistance 大于 100km 时它才会给出结果。 如果我想设置 maxdistance = 10 * 1000;那么它没有给出任何结果,但数据库中有可用数据。
这是模型:
const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const lawyerSchema = new mongoose.Schema({
name: {
type: String,required: true
},email: {
type: String,password: {
type: String,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 });
}
}
);
});
这是数据库中的记录:
这是我的邮递员:
如果我犯了任何错误,请纠正我。 谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)