简体   繁体   English

如何使用 pymongo 查找 2 个特定日期的数据

[英]How to look up data of 2 specific dates with pymongo

I´m pretty new to MongoDB and have of course googled this problem but most of the results only refer to getting the data between 2 dates.我对 MongoDB 很陌生,当然也用谷歌搜索过这个问题,但大多数结果仅指获取两个日期之间的数据。 The Timestamp column in my db looks like this:我的数据库中的时间戳列如下所示:

TIMESTAMP :2022-08-21T08:02:21.000+00:00

I want get all documents that match a specific date.我想获取与特定日期匹配的所有文件。 I know that the timedelta is a weird way of doing this but I wanted to play around a bit.我知道 timedelta 是一种奇怪的方式,但我想玩一下。 Anyway my problem is that the code below gives me exactly 1 document even though there should be houndreds.无论如何,我的问题是,即使应该有数百个,下面的代码也正好给了我 1 个文档。 Another problem is that I haven´t found a solution to bypass the need for a start and end time for both dates so that I can just use另一个问题是我还没有找到一个解决方案来绕过两个日期的开始和结束时间的需要,这样我就可以使用

"$and":[ {"TIMESTAMP": {'$eq': today, '$eq': past}]

Code:代码:

def delta(timeframe):
        from datetime import datetime, timedelta
        #subtract time
        search_column = "TIMESTAMP"
    
        if timeframe == "1":
            today = datetime.today()    
            past = today - timedelta(days=1)
            #format dates
            today_end = datetime.strftime(today, '%Y-%m-%d' + " 23:59:59")
            today_start = datetime.strftime(today, '%Y-%m-%d' + " 00:00:00")
            past_start = datetime.strftime(past, '%Y-%m-%d' + " 00:00:00")
            past_end = datetime.strftime(past, '%Y-%m-%d' + " 23:59:59")
            print(past_end,past_start)
            #convert strings to datetime
            past_start = datetime.strptime(past_start, "%Y-%m-%d %H:%M:%S")
            today_start = datetime.strptime(today_start, "%Y-%m-%d %H:%M:%S")
            past_end = datetime.strptime(past_end, "%Y-%m-%d %H:%M:%S")
            today_end = datetime.strptime(today_end, "%Y-%m-%d %H:%M:%S")
    
          
            a = collection.find({"$and":[ {"TIMESTAMP": {'$lt': today_end, '$gte': today_start},"TIMESTAMP": {'$lt': past_end, '$gte': past_start}}]})
            list_cur = list(a) #convert the cursor to the list of dictionary.
            df = pd.DataFrame(list_cur)
            #b= json.loads(dumps(a))
            print(df)

Thanks:)谢谢:)

Your curly brackets are misplaced.你的大括号放错了位置。 $and and $or operator, contains a list of conditions. $and$or运算符,包含条件列表。 So each of your condition should be a new element in the array.因此,您的每个条件都应该是数组中的一个新元素。

{"$or":[ 
    {"TIMESTAMP": {'$lt': today_end, '$gte': today_start}},
    {"TIMESTAMP": {'$lt': past_end, '$gte': past_start}}
]}

This can be translated to:这可以翻译为:

I want all the documents where (TIMESTAMP is between today_end and today_start) or (TIMESTAMP is between past_end and past_start)我想要(TIMESTAMP 在 today_end 和 today_start 之间)或(TIMESTAMP 在 past_end 和 past_start 之间)的所有文档

PS: I think you need to use $or PS:我认为你需要使用$or

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

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