简体   繁体   English

Pymongo:以日期为字符串的聚合日期范围查询

[英]Pymongo : Aggregate Date Range Query with Date as String

I have a collection as below: sample {'_id': {'day': '2020-12-04', 'device': {'device': 'DH002'}}, 'value': {'average': 44.5, 'max': 50, 'min': 38}}我有一个如下集合:示例 {'_id': {'day': '2020-12-04', 'device': {'device': 'DH002'}}, 'value': {'average': 44.5,“最大”:50,“最小”:38}}

Issue a: Am trying to query this collection basis the date range inputted by user.The query is as below.The issue is that I get output only for the start date instead of the entire range upto the end date.问题a:我正在尝试根据用户输入的日期范围查询此集合。查询如下。问题是我得到output 仅用于开始日期,而不是直到结束日期的整个范围。 Not sure what is wrong.不知道出了什么问题。

Issue b.问题 b。 If I wanted to add Device id as another query parameter,how can i do it..如果我想添加设备 ID 作为另一个查询参数,我该怎么做..


extract = list( weather_dbh.dailyreports.aggregate([
    { "$match": {
    "$expr": {
      "$and": [
        {
          "$gte": [
            { "$dateFromString": { "dateString": "$_id.day", "format": "%Y-%m-%d" }},
            start
          ]
        },
        {
          "$lt": [
            { "$dateFromString": { "dateString": "$_id.day", "format": "%Y-%m-%d" }},
            end
          ]
        }
      ]
    }
  }}
]))

The following work as expected:以下工作按预期进行:

 mongos> var start="2020-12-05"
 mongos> var end="2020-12-06"
 mongos> var thedevice="devid"
 mongos> db.weather_dbh.dailyreports.aggregate([  {$match:{ deviceid:thedevice  , $and:[  {"_id.day":{$gte:start}}, {"_id.day":{$lt:end }}      ] } }  ])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM