【发布时间】:2016-03-01 10:46:36
【问题描述】:
在我的项目中有两个需求
第一个:我有下面的集合
{
"name": "James",
"loc" : [ 12.9000, 14.6733]
},
{
"name": "James",
"loc" : [ 54.9000, 78.6733]
}
为此,我必须找到与我的位置匹配的所有位置和特定半径 所以我使用这个查询:
var myLoc = [ 13.5, 67.5 ];
var myRadius = 150;
model.find({
loc : {
$geoWithin : {
$centerSphere : [ myLoc, myRadius ]
}
}
}
).exec()
上述查询工作正常。
第二个:我有如下收藏。我遇到了问题。
{
"name" : "James",
"loc" : [ 12.9000, 14.6733],
"radius" : 115
},
{
"name" : "James",
"loc" : [ 54.9000, 78.6733],
"Radius" : 276
}
这里我的收藏本身就有半径。 用户输入仅是 loc
var myLoc = [ 14.67, 56.78 ];
现在我必须找到所有与我的位置相匹配的文档,它们的位置和半径。
我尝试过类似的查询
model
.where('loc').within({ center: myLoc, radius: 'this.Radius', unique: true, spherical: true })
.exec()
Error : The err is CastError: Cast to number failed for value "this.Radius" at path "undefined"
然后
model
.find(
{
$where:
{
Location : {
$geoWithin :
{ $centerSphere : [myLoc, '$Radius'] }
}
}
}
)
.exec()
Error : Error: Must have a string or function for $where
然后
model
.aggregate(
{
$match : {
loc : {
$geoWithin : {
$centerSphere : [ myLoc, "$Radius" ]
}
}
}
}
)
.exec( )
错误:错误是 MongoError:异常:错误查询:BadValue 错误地理查询:{ $geoWithin: { $centerSphere: [ [ -1.965373600000021, 52.4744464 ], "$Radius" ] } }
我对 mongodb 查询很陌生。即使您的小建议也会对我有很大帮助。 请告诉我如何实现这一目标。 非常感谢您的帮助。谢谢
【问题讨论】:
-
您好,对于同一个用例,我遇到了同样的问题,您是如何解决的?以下答案已弃用
标签: node.js mongodb mongoose geospatial