简体   繁体   中英

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}}

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. Not sure what is wrong.

Issue b. If I wanted to add Device id as another query parameter,how can i do it..


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 }}      ] } }  ])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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