Mongo:总计$ geoNear和$ text没有结果

我正在尝试在Mongoose中执行geoNear +文本search聚合查询:

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会返回正确的结果。 我链接查询不正确?

只有最初的$match阶段才能使用索引,所以不能在第二个$match使用文本索引。 您也不能合并使用2dsphere索引和在相同的$match使用文本索引。 一个select是切换文本search$match阶段和$geoNear阶段的$geoNear 。 交换,文本search将使用文本索引, $geoNear将仍然工作,如果你设置spherical : false$geoNear将计算平面,而不是球面,距离,并不会使用索引。

如果这样做不可行,那么如果您描述用例,我们可以尝试考虑其他选项。

或者@ wdberkeley的答案,你可以使用$geoWithin而不是$geoNear阶段。

 db.landmarkSchema.aggregate([ {$match: { $text: {$search: "great test text"} , loc: {$geoWithin: {$centerSphere: [[ 14.3, 48.3], 5/6731]}} }}]) 

注意:地理索引不会被使用!

更多信息: http : //docs.mongodb.org/manual/reference/operator/query/geoWithin/