Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

python - 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
          ]
        }
      ]
    }
  }}
]))
question from:https://stackoverflow.com/questions/65860476/pymongo-aggregate-date-range-query-with-date-as-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...