【发布时间】:2015-01-06 03:46:48
【问题描述】:
这是我的架构:
var ActivitySchema = new Schema({
loc: {type: [Number], index: '2dsphere'}
});
当我尝试查找文件时:
Activity.find().where('loc').near({
center: [-71.072810, 42.370227],
spherical: true
}).exec(function(err, docs){
if (err) console.log(err);
console.log(docs);
})
这是我得到的:
{ [MongoError:无法执行查询:错误处理查询: ns=bunch-test.activities 限制=0 跳过=0 树:GEONEAR 字段=loc maxdist=1.79769e+308 isNearSphere=1 排序:{} 项目:{} 规划器 返回错误:无法找到 $geoNear 查询的索引] 名称: 'MongoError' }
我尝试过以多种方式定义 loc。例如:
loc: {
type: {
type: String,
enum: 'Point',
default: 'Point'
},
coordinates: {
type: [Number],
default: [0,0],
}
}
还有多种查询方式:
Activity.find({
loc: {
$nearSphere: {
$geometry: {
type: "Point" ,
coordinates: [ -71.072810, 42.370227 ]
},
$minDistance: 0,
$maxDistance: 10000,
}
}
})
或:
Activity.geoNear({
type: "Point" ,
coordinates: [ -71.072810, 42.370227 ]
},
{ maxDistance : 99999999999,
spherical : true
},
function(err, results, stats) {
console.log(results);
console.log(err);
});
但结果总是相同的消息:
找不到 $geoNear 查询的索引
有什么建议吗?谢谢。
【问题讨论】:
标签: node.js mongodb express mongoose geo