MongoDB GeoJSON,查询两个点或线附近

3

我有这样一个集合:

origin: {
    location : {
        type: String,
        coordinates: [lgnt,lat]
    }
},
destination: {
    location : {
        type: String,
        coordinates: [lgnt,lat]
    }
}

在 location.origin 和 location.destination 上创建一个索引:'2dsphere'。

然后我得到了另一个“from”点和“to”点,并尝试获取数据库中满足以下条件的所有文档:

  • 起点在“from”点附近
  • 终点在“to”点附近。

对于其中的一个或另一个,都可以:

db.find({
   'origin.location': {
       $nearSphere: {
           $geometry: {
               type: "Point",
               coordinates: [from.lng, from.lat]
           },
           $maxDistance: 10
       }
   }
})

然而在文档中,写道我不能使用两个$near查询。我尝试了一些方法,比如:

db.find({
  'origin.location': {
    $nearSphere: {
      $geometry: {
        type: "Point",
        coordinates: [from.lng, from.lat]
      },
      $maxDistance: 10
    }
  },
  'destination.location': {
    $nearSphere: {
      $geometry: {
        type: "Point",
        coordinates: [to.lng, to.lat]
      },
      $maxDistance: 10
    }
  }
});

使用了两个 $near/$nearSphere 无法正常工作。 因此,我尝试使用


$and : [
    $near: ...,
    $near: ...
]

这也不起作用。我尝试使用聚合,但是不能在$match中使用$near。你会怎么做?

  • 也许我错过了什么?
  • 我可以使用LineString并尝试查询“相似的”LineString,但如何实现呢?
  • 目前的解决方案是进行两个查询,一个查询起点,一个查询终点,然后在客户端交集两个结果数组,以获得所需结果,但这需要进行两个查询。

P.S:我也尝试过:

var origines = db.find({
  'origin.location': {
    $nearSphere: {
      $geometry: {
        type: "Point",
        coordinates: [from.lng, from.lat]
      },
      $maxDistance: 10
    }
  }
}, {
  _id: 1
})
origines = origines.map(function(elem) {
  return elem._id;
});
db.find({
  $and: [{
    _id: {
      $in: origines
    }
  }, {
    'destination.location': {
      $nearSphere: {
        $geometry: {
          type: "Point",
          coordinates: [to.lng, to.lat]
        },
        $maxDistance: 10
      }
    }
  }]
});

但是没有


嘿,@Dakkon_jareth,你找到解决方案了吗? - demsey
@demsey 不,目前似乎在Mongo中不可能实现。所以我使用了两个查询。但后来发现性能问题。所以我只能使用其他解决方案。 - roro_57
感谢 @Dakkon_jareth - demsey
1个回答

0
自MongoDB 4.2以来,您可以计算到目的地的距离,然后使用匹配功能。
db.getCollection('test').aggregate([
    {
        $geoNear: {
            near: {
                type: "Point",
                coordinates: [ from.lng, from.lat ]
            },
            distanceField: "distanceToOrigin",
            maxDistance: 10,
            spherical: true
        }
    },
    {
        $addFields: {
            "distanceToDestination" : {
                $multiply: [
                    6371000,
                    { $acos: { $add: [
                        { $multiply: [
                            { $sin: { $degreesToRadians: {"$arrayElemAt": ["$destination.location.coordinates", 1]} } },
                            { $sin: { $degreesToRadians: to.lat } }
                        ]},
                        { $multiply: [
                            { $cos: { $degreesToRadians: {"$arrayElemAt": ["$destination.location.coordinates", 1]} } },
                            { $cos: { $degreesToRadians: to.lat } },
                            { $cos: { $degreesToRadians: { "$subtract": [ { "$arrayElemAt": ["$destination.location.coordinates", 0] }, to.lng ] } } }
                        ]}
                    ] } }
                ]
            }
        }
    },
    {
        $match: { "distanceToDestination": {$lt: 10} }
    }
])

我喜欢这个想法,但是用这个公式计算距离时出现了错误(对于应该为0的距离,结果超过了700000米,目的地=to)。有人成功解决了这个问题吗? - Vincent J

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接