gpt4 book ai didi

node.js - 如何使用mongoose在mongodb中使用纬度和经度查找附近的点

转载 作者:行者123 更新时间:2023-12-05 07:08:57 26 4
gpt4 key购买 nike

架构:

const locationSchema = new mongoose.Schema({
id:String,
name:{type: String , required:true},
address:String,
rating:{type:Number , "default":0, min:0 , max:5 },
facilities:[String],
// geoJSON schema 1
coords:
{
type: {type: String},
coordinates:[]

},

openingTimes: [openingTimeSchema],
reviews: [reviewSchema]
});

locationSchema.index({'coords':'2dsphere'})

端点:http://localhost:3000/api/locations?lng=-0.7992599&lat=51.378091

const theEarth = (function() {
console.log('theEarth');
const earthRadius = 6371; // km, miles is 3959

const getDistanceFromRads = function(rads) {
return parseFloat(rads * earthRadius);
};

const getRadsFromDistance = function(distance) {
return parseFloat(distance / earthRadius);
};

return {
getDistanceFromRads: getDistanceFromRads,
getRadsFromDistance: getRadsFromDistance
};
})();

module.exports.locationsListByDistance = (req,res) => {

const longitude = parseFloat(req.query.lng);
const latitude = parseFloat(req.query.lat);
//const maxDistance = parseFloat(req.query.maxDistance);
Loc.aggregate(
[{
$geoNear: {
near: { type: "Point", coordinates: [ longitude , latitude ] },
distanceField: "coords.calculated", // required
maxDistance: theEarth.getRadsFromDistance(20),
includeLocs: "coords.location",
spherical:true


}

}
],function(err,results){
console.log(results);
});




sendJsonResponse(res,200,results);

};

错误:

UnhandledPromiseRejectionWarning: MongoError: There is more than one 2dsphere index on Loc8r.locations; unsure which to use for $geoNear
at MessageStream.messageHandler (E:\Udemy\NodeTutorial\getting-mean\loc8r\node_modules\mongodb\lib\cmap\connection.js:261:20)
at MessageStream.emit (events.js:198:13)
at processIncomingData (E:\Udemy\NodeTutorial\getting-mean\loc8r\node_modules\mongodb\lib\cmap\message_stream.js:144:12)
at MessageStream._write (E:\Udemy\NodeTutorial\getting-mean\loc8r\node_modules\mongodb\lib\cmap\message_stream.js:42:5)
at doWrite (_stream_writable.js:415:12)
at writeOrBuffer (_stream_writable.js:399:5)
at MessageStream.Writable.write (_stream_writable.js:299:11)
at Socket.ondata (_stream_readable.js:709:20)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
(node:5040) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async
function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
GET /bootstrap/bootstrap.min.css 304 0.954 ms - -
GET /stylesheets/style.css 304 5.782 ms - -
GET /api/bootstrap/jquery.min.js 404 65.965 ms - 3123
GET /api/bootstrap/bootstrap.min.js 404 116.055 ms - 3123

我正在使用上述方法来查找距离接近端点中查询参数中的经度和纬度的文档,但结果返回未定义,请告诉我如何更正此错误。

最佳答案

module.exports.locationsListByDistance = (req,res) => {

const longitude = parseFloat(req.query.lng);
const latitude = parseFloat(req.query.lat);
//const maxDistance = parseFloat(req.query.maxDistance);
// const options = {near :[longitude,latitude],maxDistance:10000}
Loc.find({

coords: {
$near : {


$maxDistance: 10,
$geometry : {
type : 'Point',
coordinates:[longitude,latitude]
}
}
}
}).find((error,results)=>{
if (error) console.log(error);
//console.log(JSON.stringify(results, 0, 2));
sendJsonResponse(res,200,results);
});

};

这工作正常。

关于node.js - 如何使用mongoose在mongodb中使用纬度和经度查找附近的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61779937/

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