【问题标题】:Query by distance and date with Pymongo [duplicate]使用 Pymongo 按距离和日期查询 [重复]
【发布时间】:2019-07-30 00:56:58
【问题描述】:

我知道这是一个非常基本的问题,但我不明白该怎么做。

我在 Python 3 和 MongoDB 3.4.19 中使用 pymongo 3.6.1。我正在尝试从数据库中检索低于某个日期的记录,按距离排序。通过单独的功能是:

db[database].MIDAS_stations.find(
        {
            "loc": {
                "$near": {
                  "$geometry": {
                    "type": "Point",
                    "coordinates": coords
                  }
                }
              }
        }
    )

db[database].MIDAS_stations.find(
        {
          "Station start date": {
            "$gt": date1
          }
        }
    )

我尝试过使用聚合而不是查找,但我仍然没有找到这样做的方法。

我知道MongoDB/PyMongo: Querying multiple criteria - unexpected results 有类似的回答,但我的问题不同,因为它使用了地理参考数据。

来自MongoDB/PyMongo: Querying multiple criteria - unexpected resultshttps://stackoverflow.com/a/23577413/2313887我认为这个问题的答案是:

db[database].MIDAS_stations.find(
    {            
        "loc": {
            "$near": {
                "$geometry": {
                    "type": "Point",
                    "coordinates": coords
                }
            }
        },
        "Station start date": {
                "$lt": date1
            }
    }
)

但我仍然不确定这是否是正确的方法,或者它是否真的在查询最近的条目,然后只选择日期低于 date1 的条目。

【问题讨论】:

标签: python mongodb pymongo


【解决方案1】:

我认为这就是答案:

to_check= db[database].MIDAS_stations.find(
    {
        "$and": [
            {
              "Station start date": {
                '$gt': date1
              }
            },
            {
                'loc': {
                    '$near': {
                      '$geometry': {
                        'type': "Point",
                        'coordinates': coords
                      }
                    }
                  }
            }
        ]
    }
)

【讨论】:

  • 好吧,您可能已经从stackoverflow.com/a/23577413/2313887 那里了解到,这里根本不需要$and。这也与“排序条件”无关。
  • 我刚刚写了我认为这可能是答案,没有这样标记,因为我知道这可能是错误的。我在发布我的问题之前看到了该链接,知道不需要 $and。我想我应该使用$sort,但我不知道如何使用几何类型。我唯一能找到的是$near 按距离返回结果。正如我在最初的问题中所说,我知道这是基本问题,但我不知道该怎么做,昨天我花了一整天的时间阅读试图找到答案。
猜你喜欢
  • 1970-01-01
  • 2016-09-25
  • 2020-05-16
  • 2017-07-22
  • 2021-11-14
  • 2021-07-20
  • 2017-01-17
  • 2014-07-09
  • 2014-12-09
相关资源
最近更新 更多