Node.js 无法在node express中使用带字符串错误的$near
当我们使用maxDistance查找基于经度和纬度的记录时,我发现错误无法在node express中使用带字符串的$near 以下是我正在使用的代码:- 1-模式代码:-Node.js 无法在node express中使用带字符串错误的$near,node.js,mongodb,express,Node.js,Mongodb,Express,当我们使用maxDistance查找基于经度和纬度的记录时,我发现错误无法在node express中使用带字符串的$near 以下是我正在使用的代码:- 1-模式代码:- var mongoose = require('mongoose'), Schema = mongoose.Schema; var dtSchema = new Schema({ name:String, date: {type: Date, default: Date.now}, locati
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var dtSchema = new Schema({
name:String,
date: {type: Date, default: Date.now},
location: { type: String, coordinates: [Number] },
category:String
});
//dtSchema.index({location: '2dsphere'});
module.exports = mongoose.model('places', dtSchema);
用于获取记录的2-server.js代码
app.get('/location/:lon/:lat', function(req, res) {
console.log(req.params.lon);
console.log(req.params.lat);
location.find({location:
{ $near:
{
$geometry: { type: "Point", coordinates: [req.params.lon, req.params.lat ] },
$maxDistance: 5000
}
}}, function(err, places) {
if(!err){
res.send('({"records":' + JSON.stringify(places) + '});');
}
else{
console.log(err);
res.send("error coming")
}
});
});
参数发送URL
当我们执行代码并点击上面的URL时。我发现错误不能在node express中使用带字符串错误的$neardb.location.aggregate[{$geoNear:{near:{type:Point,coordinates:[72.4468035,23.032069]},maxDistance:500,Sphereal:true,distanceField:distance}]我刚才遇到过同样的问题。错误在架构声明中 var dtSchema=新模式{ 名称:String, 日期:{type:date,默认值:date.now}, 地点:{ 类型:{ 类型:字符串 }, 坐标:[数字] }, 类别:字符串
};下面提到的查询在mongo shell中工作,但不在mongoose中工作。请帮助我简短地说明问题db.location。查找{location:{$nearSphere:{$geometry:{type:Point,座标:[-73.93414657,40.82302903]},$maxDistance:500}我不明白为什么这个查询在mongoosecan中不起作用,你能给我一个例子吗?在我的查询中,当运行itdb.location.aggregate[{$geoNear:{near:{类型:点,坐标:[72.4468035,23.032069]},最大距离:500,球面:真,距离字段:距离}]