【问题标题】:Querying with MongoDB issue查询 MongoDB 问题
【发布时间】:2021-01-21 18:38:55
【问题描述】:

我正在尝试(不成功)查询具有这种结构的集合:

{
     origin : { lat : #, lng : #},
     destination : { lat : #, lng #}
}

使用此查询:


{
     origin : { lat : { $gte: in.lat-1, $lte : in.lat+1},
               lng : { $gte: in.lng-1, $lte : in.lng+1}
                },
      destination : { lat : { $gte: out.lat-1, $lte : out.lat+ 1},
                lng : { $gte: out.lng-1, $lte : out.lng +1}}
}

基本上我正在尝试查找其坐标在我的输入/输出 lats/lngs 的某个范围内(此处为 1)的文档。但是当我通过这个查询时,它与我知道它应该匹配的文档不匹配。有什么建议吗?

【问题讨论】:

    标签: node.js database mongodb search nosql


    【解决方案1】:

    我不知道它为什么会起作用,但我将起点和终点分成两个子组件(lat 和 lng)并查询那些对我有用的组件。

    【讨论】:

      【解决方案2】:

      要请求子文档,您必须执行origin.lat { $gte ...}

      db.collection.find({
        "origin.lat": {
          "$gte": 7,
          "$lte": 10
        },
        "origin.lng": {
          "$gte": 7,
          "$lte": 10
        },
        "destination.lat": {
          "$gte": 7,
          "$lte": 10
        },
        "destination.lng": {
          "$gte": 7,
          "$lte": 10
        }
      })
      

      试试here

      【讨论】:

        猜你喜欢
        • 2017-06-01
        • 1970-01-01
        • 2016-01-29
        • 2021-12-14
        • 1970-01-01
        • 1970-01-01
        • 2013-09-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多