使用日期范围的pymongo查询

时间:2017-03-02 23:59:08

标签: mongodb pymongo

我有mongodb集合名为jobs

open System
open FSharp.Data
open FSharp.Data.JsonExtensions


#if INTERACTIVE
#r @"..\packages\FSharp.Data.2.3.2\lib\net40\FSharp.Data.dll"
#endif 


[<Literal>]
let file = __SOURCE_DIRECTORY__ + @"\file1.csv"
[<Literal>]
let path3 = __SOURCE_DIRECTORY__
[<Literal>]
let path4 = "."

type SampleFile = CsvProvider<file,HasHeaders=true>
type SampleFile3 = CsvProvider<"file1.csv",HasHeaders=true,ResolutionFolder=path3>


[<EntryPoint>]
let main argv = 

    //let nonLiteralPath = @".\file1.csv" // you could hardcode this in the file but:
    let nonLiteralPath = argv.[0]  // you can also use a path specified on the command line
    let DataFile = SampleFile.Load(nonLiteralPath)
    [for row in DataFile.Rows -> row.``Key #1``]  |> printfn "%A"
    let x= SampleFile3.GetSample()  // use a relative path, this will be the root of the project at design time
                                    // or the root of the exe at the execution time
    [for row in x.Rows -> row.``Key #2``] |> printfn "%A"   

    printfn "%A" argv

我正在使用pymongo,找到两个日期之间的工作

{"_id":  "1", "jobUploadDate":  "2017-02-01T14:30:57.361Z"}, 
{"_id":  "2", "jobUploadDate":  "2017-02-02T14:31:26.497Z"},
{"_id":  "3", "jobUploadDate":  "2017-02-03T15:04:45.064Z"}

我期待输出如下,但它返回NO结果。

def find_jobs(self,from_date,to_date):
    to_date = datetime.datetime.strptime(to_date, "%Y%m%d").isoformat()
    from_date = datetime.datetime.strptime(from_date, "%Y%m%d").isoformat()
    query = {}
    query['jobUploadDate'] = {'$lte': to_date,'$gte':from_date}

    #self.db = .....get mongodb connection and 

    jobs_found = self.db.jobs.find(query, {
        '_id': 1,
        'jobUploadDate': 1})

    return dumps(list(jobs_found))

print(find_jobs("20170202","20170202"))

如何投射&#34; jobUploadDate&#34;到目前为止并剥夺时间并进行比较?

1 个答案:

答案 0 :(得分:0)

用于去除日期时间的日期格式为日期分配零时间,请测试:

dstring = "20170202"
d = datetime.strptime(dstring, "%Y%m%d")
print(d)

2017-02-02 00:00:00

因此,您应该致电find_jobs("20170202","20170203")一天所需的范围。