![](/img/trans.png)
[英]Getting Error while fetching data location wise with $near query in mongoose
[英]Mongoose $near query returns parse error
這是一個對象示例:
{
"_id" : "581994",
"type" : "Feature",
"geometry" : {
"type" : "Point",
"coordinates" : [
-149.0133,
64.7439
]
}
}
這是我執行的查詢:
Earthquake
.find({
geometry: {
$near: {
$geometry: {
type: 'Point',
coordinates: [lon, lat]
}
}
}
})
.exec(function(err, results) {
if (err) {
console.log(err);
}
return reply(results);
})
這是我創建的模型架構:
var mongoose = require('mongoose');
mongoose.set('debug', true);
var Schema = mongoose.Schema;
var earthquakeSchema = new Schema({
geometry: {
type: String,
coordinates: [Number]
}
});
earthquakeSchema.index({
geometry: '2dsphere'
});
var Earthquake = mongoose.model('Earthquake', earthquakeSchema);
module.exports = Earthquake;
從我的角度看,這似乎是正確的,但是當我執行它時,我總是得到相同的錯誤:
[Error: Can't use $near with String.]
我沒有發現錯誤在哪里。 我到處都檢查過
好的,我找到了解決方案:
我必須將與“點”相對應的默認字段添加到屬性“類型”中。 現在可以了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.