【问题标题】:Mongo: aggregate $geoNear and $text no resultsMongo:聚合 $geoNear 和 $text 没有结果
【发布时间】:2015-04-25 09:22:47
【问题描述】:

我正在尝试在 Mongoose 中进行 geoNear + 文本搜索聚合查询:

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 会返回正确的结果。我是否错误地链接了查询?

【问题讨论】:

  • 来自 mongodb IRC 的一位用户说:“只有一个索引可以用于聚合”——任何人都有这种查询的最佳实践吗?

标签: node.js mongodb mongoose mongodb-query


【解决方案1】:

除了@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/

【讨论】:

    【解决方案2】:

    只有初始的$match 阶段可以使用索引,因此您不能在第二个$match 中使用文本索引。您也不能在同一个$match 中结合使用 2dsphere 索引和文本索引。一种选择是切换文本搜索$match 阶段和$geoNear 阶段的顺序。交换后,文本搜索将使用文本索引,如果您设置 spherical : false$geoNear 仍然可以工作。 $geoNear 将计算平面距离,而不是球面距离,并且不会使用索引。

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-10
    • 2015-12-21
    • 1970-01-01
    • 2023-03-21
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    相关资源
    最近更新 更多