pymongo bson.code传入变量

时间:2016-02-26 22:01:02

标签: mongodb scope mapreduce pymongo

我正在使用pymongo的map_reduce框架从mongodb中提取一些数据。这就是我的数据的样子:

{
    "_id" : ObjectId("566f570e3816dc2fe631db4f"),
    "property_id" : 5594.0000000000000000,
    "reservation_id" : "2544430.1",
    "updated" : ISODate("2015-12-15T02:04:33.000Z"),
    "offer_list" : {
        "68799" : {
            "pitched" : "no",
            "accepted" : "no"
        },
        "68801" : {
            "pitched" : "no",
            "accepted" : "no"
        }
    },
    "status" : "awarded",
    "comments" : "",
    "agent_id" : 1.0000000000000000,
    "created" : ISODate("2015-12-14T23:55:52.000Z")
}

我需要在使用pymongo的某个日期之后获得每个代理的所有记录计数。

映射器函数如下所示:

mapper = Code("""function(){
        if(this.created>start_date){
             emit(this.agent_id, 1); 
        }) 

其中start_date是从python代码传入的变量。

通过阅读文档,我认为范围应该设置,但我找不到任何关于如何做到这一点的文档。我对javascript一无所知。有人可以帮忙吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

使用query关键字修复了该问题。

collection.map_reduce(mapper, reducer, "myresults", query={"created":{"$gte":start_date, "$lt":end_date}})