Mongo:聚合 $geoNear 和 $text 没有结果 [英] Mongo: aggregate $geoNear and $text no results
问题描述
我正在尝试在 Mongoose 中进行 geoNear + 文本搜索聚合查询:
I'm trying to do a geoNear + text search aggregate query in Mongoose:
landmarkSchema.aggregate(
[
{ "$geoNear": {
"near": {
"type": "Point",
"coordinates": [parseFloat(userCoord1), parseFloat(userCoord0)]
},
"distanceField": "distance",
"minDistance": 1,
"maxDistance": 5000,
"spherical": true,
"query": { "loc.type": "Point" }
} },
{ $match: { $text: { $search: sText } } },
{ $sort: { score: { $meta: "textScore" } } }
],
function(err,data) {
if (data){
res.send(data);
}
else {
console.log('no results');
res.send({err:'no results'});
}
});
但是 Mongo 没有返回任何结果.当我分别执行每个查询时,$geoNear
和 $match : $text
会返回正确的结果.我是否错误地链接了查询?
But Mongo is not returning any results. When I perform each query separately, $geoNear
and $match : $text
the correct results are returned. Am I chaining the query incorrectly?
推荐答案
只有初始 $match
阶段可以使用索引,所以不能在第二个 $match 使用文本索引代码>.您也不能在同一个
$match
中结合使用 2dsphere 索引和使用文本索引.一种选择是切换文本搜索 $match
阶段和 $geoNear
阶段的顺序.交换后,文本搜索将使用文本索引,并且如果您设置 sphereal : false
,$geoNear
仍然可以工作.$geoNear
将计算平面距离,而不是球面距离,并且不会使用索引.
Only an initial $match
stage can use an index, so you cannot use a text index in the second $match
. You also can't combine using a 2dsphere index and using a text index in the same $match
. One option is switching the order of the text search $match
stage and the $geoNear
stage. Swapped, the text search will use a text index and $geoNear
will still work if you set spherical : false
. $geoNear
will calculate planar, not spherical, distances, and will not use an index.
如果这不可行,如果您描述用例,我们可以尝试考虑其他选项.
If that's not feasible, we could try to think of other options if you describe the use case.
这篇关于Mongo:聚合 $geoNear 和 $text 没有结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!