我试图搜索靠近某个地点的“地点”,并满足以下一些其他标准:
var places = Places.find(
{
$and :
[
{ categories : placesCategory },
{
$or :
[
{ name : { $regex : searchQuery, $options : 'i' } },
{ city : { $regex : searchQuery, $options : 'i' } },
{ country : { $regex : searchQuery, $options : 'i' } }
]
},
{
location :
{
$near : [ nearLng, nearLat ],
$maxDistance : 67.15 // radians
}
}
]
}).fetch();然而,控制台告诉我"$near不能在另一个$操作符中“。
是否有一种方法可以将$near包含在$and中?
或者最好的做法是什么?
发布于 2015-01-24 21:47:35
不需要在$near语句中包含$and查询。你可以把它放在外面,它还能用。
var places = Places.find(
{
location :
{
$near : [ nearLng, nearLat ],
$maxDistance : 67.15 // radians
},
$and :
[
{ categories : placesCategory },
{
$or :
[
{ name : { $regex : searchQuery, $options : 'i' } },
{ city : { $regex : searchQuery, $options : 'i' } },
{ country : { $regex : searchQuery, $options : 'i' } }
]
}
]
}).fetch();但是,我不确定您的$near查询是否正常工作。我更喜欢在坐标上使用in '2dsphere‘的更具体的查询。例如:
location:
{
$near:
{
$geometry:
{
type: 'Point',
coordinates: [ nearLng, nearLat ]
},
$maxDistance: 67.15 // radians
}
}编辑:为了使用上述查询,请确保根据此模型保存数据:
location: {
type: {
type: String,
enum: 'Point',
default: 'Point'
},
coordinates: {
type: [Number],
default: [0,0],
index: '2dsphere'
}
}当您想在矩形中找到位置时,您可能需要$box操作符。这是一个示例搜索查询(请注意,我没有使用它的经验。您可能需要更改模型和/或添加索引以改进查询):
location: {
$geoWithin: {
$box: [
[ <bottom left coordinates> ],
[ <upper right coordinates> ]
]
}
}
}如果这有帮助的话请告诉我!
https://stackoverflow.com/questions/28127240
复制相似问题