【问题标题】:Pymongo query with $lte and $gte not working使用 $lte 和 $gte 的 Pymongo 查询不起作用
【发布时间】:2017-09-21 12:33:19
【问题描述】:

我在 mongo 中有以下文档:

{
    'Name': 'Dummy',
    'North-East-Bound': {
        'lat': 0,
        'lng': 0
    },
    'South-West-Bound': {
        'lat': 0,
        'lng': 0
    }
}

我正在执行以下查询:

result = self.coll.find_one({
            'North-East-Bound':
                {'lat': {'$gte': lat},
                 'lng': {'$gte': lng}
                 },
            'South-West-Bound':
                {'lat': {'$lte': lat},
                 'lng': {'$lte': lng}
                 }
        })

显然我使用 lat=0 和 lng=0 作为参数。我希望返回虚拟文档,但我只得到无。我做错了什么?

谢谢。

【问题讨论】:

    标签: python mongodb pymongo database


    【解决方案1】:

    您必须使用点符号对嵌入文档的字段运行查询。

     find_one({
          'North-East-Bound.lat': {
              '$gte': lat
          },
          'North-East-Bound.lng': {
              '$gte': lng
          },
          'South-West-Bound.lat': {
              '$lte': lat
          },
          'South-West-Bound.lng': {
              '$lte': lng
          }
      })
    

    这将适用于相等比较。这是嵌入式文档级别的比较。

    find_one({
        'North-East-Bound': {
            'lat': 0,
            'lng': 0
        },
        'South-West-Bound': {
            'lat': 0,
            'lng': 0
        }
    })
    

    这里有更多

    https://docs.mongodb.com/manual/core/document/#dot-notation

    https://docs.mongodb.com/manual/tutorial/query-embedded-documents/#query-on-nested-field

    【讨论】:

      猜你喜欢
      • 2016-12-23
      • 2012-12-04
      • 2015-12-27
      • 2022-12-05
      • 2016-11-17
      • 2013-06-07
      • 1970-01-01
      • 2020-08-19
      • 2023-04-09
      相关资源
      最近更新 更多