我使用猫鼬执行地理多边形搜索,如下所示:
Location.find({
"location.coordinates": {
"$geoWithin": {
"$geometry": {
"type": "Polygon",
"coordinates": [
coords
]
}
}
}
}, cb);这将返回一组记录,如:
{
"_id": "544be0763cea87660ee9c989",
"rating": 5,
"__v": 0,
"create_date": "2014-10-25T17:40:06.167Z",
"location": {
"coordinates": [
-122.41941550000001,
37.7749295
],
"type": [
"Point"
]
}
},
{
"_id": "544be0763cea87660ee9c989",
"rating": 7,
"__v": 0,
"create_date": "2014-09-27T01:40:10.283Z",
"location": {
"coordinates": [
-122.41941550000001,
37.7749295
],
"type": [
"Point"
]
}
}我还需要计算“评级”属性的平均值。我知道Mongo有一个聚合框架,但我不确定这是否可以作为初始搜索的一部分来完成,还是作为一个辅助查询来完成,或者是作为后置处理结果。
有人能告诉我吗?
更新:为了明确起见,我想返回当前的结果集,以及整个结果集的平均评分(如果可能的话)。
发布于 2014-11-06 03:21:37
通过使用的查询语法,可以清楚地在服务器上使用MongoDB 2.6或更高版本。这基本上形成了您的答案,就像在这个版本中一样,at查询引擎是统一的,因此GeoSpatial查询与其他查询在相同的空间中运行。
这意味着聚合框架也可以使用完全相同的查询,因为这使用了常规查询本身:
Location.aggregate(
[
// Perform query
{ "$match": {
"location.coordinates": {
"$geoWithin": {
"$geometry": {
"type": "Polygon",
"coordinates": [
coords
]
}
}
}
}},
// Get average from results
{ "$group": {
"_id": null,
"avgRating": { "$avg": "$rating" }
}}
],
function(err,results) {
}
);大多数GeoSpatial查询都需要处于第一个流水线阶段,实际上有一个特殊的阶段来预测与$geoNear的距离。但是我认为$geoWithin在以后的管道阶段是很好的,但是在它可以使用索引的地方当然是最优的。这意味着只在第一阶段。
https://stackoverflow.com/questions/26771109
复制相似问题