在mongoose版本3.8.3中,我使用mongoDB $nearSphere方法来查找附近的位置。
mongoose.models['Event']
.find({ loc :
{
$nearSphere :
{
$geometry : loc,
$maxDistance : 100000
}
}
})
.limit(100)
.exec(function (err, events)
{
console.log(err)
...
}事件方案中的loc字段有一个'2dsphere‘索引:
var EventSchema = new Schema
({
...
loc:
{
type: { type: String },
coordinates: { type: [Number], index: '2dsphere' }
},
...
});变量loc的类型为'Point‘。下面是一个例子:
{ coordinates: [ 12.93598683338508, 48.43299197266921 ], type: 'Point' }在执行上面的搜索语句后,我总是得到错误:
2013-12-27T08:46:57.997102+00:00 app[web.1]: { message: 'Cast to number failed for value "Point" at path "__QueryCasting__"',
2013-12-27T08:46:57.997105+00:00 app[web.1]: name: 'CastError',
2013-12-27T08:46:57.997107+00:00 app[web.1]: type: 'number',
2013-12-27T08:46:57.997108+00:00 app[web.1]: value: 'Point',
2013-12-27T08:46:57.997110+00:00 app[web.1]: path: '__QueryCasting__' }有人能找出这个错误出现的原因和解决方法吗?
发布于 2014-01-13 19:57:45
这看起来像是Mongoose中的一个bug。我已经提交了一个问题:https://github.com/LearnBoost/mongoose/issues/1874
我有两个建议的解决方法。
我通过修改以下命令修补了mongoose/lib/query.js:
} else if (('$near' == geo || '$geoIntersects' == geo) &&至
} else if (('$near' == geo || '$nearSphere' == geo || '$geoIntersects' == geo) &&这对我来说很有效(mongoose 3.8.3)。
其次,您可以将查询作为“遗留”坐标对运行(您需要将maxDistance转换为弧度)。http://docs.mongodb.org/manual/reference/operator/query/nearSphere/
发布于 2014-01-25 23:31:55
Mongoose有一个内置的方法,称为near(),它接受用于球形地理空间查询的GeoJSON点。
从文档中:
指定$near或$nearSphere条件
因此,不是:
mongoose.models['Event']
.find({ loc :
{
$nearSphere :
{
$geometry : loc,
$maxDistance : 100000
}
}
}).limit(100).exec(function (err, events) { ... });试试这个:
mongoose.models['Event']
.near('loc', {
center: loc,
maxDistance : 100000
}).limit(100).exec(function (err, events) { ... });发布于 2014-06-28 04:21:21
这并不是mongoose中真正的bug,你应该在‘loc.coates’上执行find(),而不仅仅是'loc',因为这是2dsphere索引所在的字段。现在,即使mongoose在没有失败的情况下执行这个查询,mongodb服务器也会返回一个错误,因为您正试图在没有地理索引的字段上运行$nearSphere。FWIW 1Liner修复将在mongoose的下一个小版本中出现,但您也应该更改您的查询代码。
https://stackoverflow.com/questions/20797711
复制相似问题