gpt4 book ai didi

node.js - Mongoose geoNear maxDistance 结果

转载 作者:太空宇宙 更新时间:2023-11-03 22:41:14 26 4
gpt4 key购买 nike

我正在使用node.js 和mongoose。我的数据库中有 4 个带有 lng/lat 坐标的对象。在现实生活中,这些位置彼此相距不到 1 或 2 英里。

module.exports.locationsListByDistance = function(req, res) {
var lng = parseFloat(req.query.lng);
var lat = parseFloat(req.query.lat);
var point = {
type: "Point",
coordinates: [lng, lat]
};
var geoOptions = {
spherical: true,
maxDistance: 20 / 3963,
num: 10
};
Loc.geoNear(point, geoOptions, function(err, results, stats) {
var locations;
console.log('Geo Results', results);
console.log('Geo stats', stats);
if (err) {
console.log('geoNear error:', err);
sendJsonResponse(res, 404, err);
} else {
locations = buildLocationList(req, res, results, stats);
sendJsonResponse(res, 200, locations);
}
});
};

这是我的代码。当Loc.geoNear运行时,回调函数results返回一个空数组。我可以让它实际返回对象的唯一方法是设置 maxDistance: 2500,然后它将检索 2 个距离离奇的对象。

现在显然,2500 是一个巨大的数字,这毫无意义。我需要彼此的距离在 20 英里以内,这就是为什么我通过 20/3963 转换为弧度,但生成的 maxDistance 太小,以至于 mongo 没有返回任何内容。有任何想法吗?

编辑 1(添加文档)

{
"_id": {
"$oid": "5714204af42b2b9004313d7c"
},
"name": "Starcups",
"address": "125 High Street, Reading, RG6 1PS",
"rating": 3,
"facilities": [
"Hot drinks",
"Food",
"Premium wifi"
],
"coords": {
"lng": -84.135166,
"lat": 34.190996
},
"openingTimes": [
{
"days": "Monday - Friday",
"opening": "7:00am",
"closing": "7:00pm",
"closed": false
},
{
"days": "Saturday",
"opening": "8:00am",
"closing": "5:00pm",
"closed": false
},
{
"days": "Sunday",
"closed": true
}
],
"reviews": [
{
"author": "Kenny Hall",
"rating": 5,
"timestamp": "13 April 2016",
"reviewText": "What a great place. I can't say enough good things about it."
},
{
"author": "Charlie Chaplin",
"rating": 3,
"timestamp": "16 June 2015",
"reviewText": "It was okay. Coffee wasn't great, but the wifi was fast."
}
]
}

编辑 2(添加模型/架构)

var mongoose = require('mongoose');

var reviewSchema = new mongoose.Schema({
author: String,
rating: {
type: Number,
required: true,
min: 0,
max: 5
},
reviewText: String,
createdOn: {
type: Date,
"default": Date.now
}
});

var openingTimeSchema = new mongoose.Schema({
days: {
type: String,
required: true
},
opening: String,
closing: String,
closed: {
type: Boolean,
required: true
}
});

var locationSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
address: String,
rating: {
type: Number,
"default": 0,
min: 0,
max: 5
},
facilities: [String],
// Always store coordinates longitude, latitude order
coords: {
type: [Number],
index: '2dsphere'
},
openingTimes: [openingTimeSchema],
reviews: [reviewSchema]
});

mongoose.model('Location', locationSchema);

最佳答案

所以最初我认为 MongoDB 是以弧度为单位工作的,所以我一直在尝试为此进行所有这些转换。但经过更多研究后,https://docs.mongodb.com/manual/reference/command/geoNear/#dbcmd.geoNear ,它表示如果 spherical: true 那么它使用米。

所以我只是编写了一个新的转换方法,因此当我在查询中设置 km 中的 maxDistance 时,我会对其进行转换并使用 m> 相反,现在一切都很好!

var meterConversion = (function() {
var mToKm = function(distance) {
return parseFloat(distance / 1000);
};
var kmToM = function(distance) {
return parseFloat(distance * 1000);
};
return {
mToKm : mToKm,
kmToM : kmToM
};
})();

/* GET list of locations */
module.exports.locationsListByDistance = function(req, res) {
var lng = parseFloat(req.query.lng);
var lat = parseFloat(req.query.lat);
var maxDistance = parseFloat(req.query.maxDistance);
if ((!lng && lng !== 0) || (!lat && lat !== 0) || !maxDistance) {
console.log('locationsListByDistance missing params');
sendJsonResponse(res, 404, {
"message" : "lng, lat and maxDistance query parameters are all required"
});
return;
}
var point = {
type: "Point",
coordinates: [lng, lat]
};
var geoOptions = {
spherical: true,
maxDistance: meterConversion.kmToM(maxDistance),
num: 10
};
Loc.geoNear(point, geoOptions, function(err, results, stats) {
var locations;
console.log('Geo Results', results);
console.log('Geo stats', stats);
if (err) {
console.log('geoNear error:', err);
sendJsonResponse(res, 404, err);
} else {
locations = buildLocationList(req, res, results, stats);
sendJsonResponse(res, 200, locations);
}
});
};

var buildLocationList = function(req, res, results, stats) {
var locations = [];
results.forEach(function(doc) {
locations.push({
distance: meterConversion.mToKm(doc.dis),
name: doc.obj.name,
address: doc.obj.address,
rating: doc.obj.rating,
facilities: doc.obj.facilities,
_id: doc.obj._id
});
});
return locations;
};

特别感谢@Saleem 和其他帮助我找到解决方案的人。

关于node.js - Mongoose geoNear maxDistance 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37149944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com