简体   繁体   English

Mongo DB聚合日期过滤器不起作用

[英]Mongo DB aggregation date filter not working

I am trying to filter date on MongoDB via pymongo.我正在尝试通过 pymongo 过滤 MongoDB 上的日期。 Specifically looking to "match" all dates greater than a given date.专门寻找“匹配”大于给定日期的所有日期。 My query is definitely correct, as it queries perfectly in MongoDB compass - aggregations.我的查询绝对正确,因为它在 MongoDB 罗盘 - 聚合中完美查询。 However once I add this to my pymongo code it returns zero results.但是,一旦我将它添加到我的 pymongo 代码中,它就会返回零结果。 So as mentioned it did not work, so I have confirmed via the compass application, that my query string is infact correct, please assist, my code below is what I used to query in compass which works, and further below is my full aggregate pipeine in pymongo - which doesn't work.所以如上所述它不起作用,所以我通过指南针应用程序确认我的查询字符串实际上是正确的,请协助,我下面的代码是我用来在指南针中查询的代码,下面是我的完整聚合管道在 pymongo - 这不起作用。 ( BTW: My date format is stored correctly in mongo - as "date") (顺便说一句:我的日期格式正确存储在 mongo - 作为“日期”)

{
// MongoDB compass code - works fine.

"date": {"$gt": new Date("2020-12-06T00:00:00.000+00:00")}

}

//Pymongo code

betdate = weekcol.aggregate([
    ## stage 1
    {"$match":{
  
"date": {"$gt": new Date("2020-12-06T00:00:00.000+00:00")}

    }        }
        
])

for bdate in betdate:
    print(bdate)

The above pymongo code, throws me the error:上面的 pymongo 代码向我抛出了错误:

File "", line 9 "date": {"$gt": new Date("2020-12-06T00:00:00.000+00:00")} SyntaxError: invalid syntax文件 "",第 9 行 "date": {"$gt": new Date("2020-12-06T00:00:00.000+00:00")} SyntaxError: 无效语法

I had a feeling it was the new date() which was a problem, but I changed it to ISODate(), and it still doesn't work.我有一种感觉是新的 date() 有问题,但我将其更改为 ISODate(),它仍然无法正常工作。

The equivalent in pymongo is: pymongo 中的等价物是:

import datetime
import pytz

filter1 = {"date": {"$gt": datetime.datetime(2020, 12, 6, 0, 0, tzinfo=pytz.utc)}}

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

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