Monary:聚合框架,设置allowDiskUse选项

时间:2015-10-07 15:59:38

标签: mongodb aggregation-framework monary

我正在使用Monary连接到我的MongoDB。 但我正在努力弄清楚究竟在哪里以及如何设置allowDiskUse选项?

client = Monary("ip.address.of.server", 27017 , "username", "password", "dbname")

pipeline = [
        {"$group" : {
            "_id" : {"user":"$subscriber_id",
                 "month": { "$month" : "$timestamp" },
                 "day" : { "$dayOfMonth" : "$timestamp" },
                 "year" : { "$year" : "$timestamp" },
                 "hour" : { "$hour" : "$timestamp" },
                 "category":"$category_name"
                },
            "activities_sum":{"$sum":"$activity_count"}
            }
        }
    ]

with client as m:
    users, years, months, days, hours, categories, activities  = m.aggregate("digicel_exploration",
                "5_min_slots",
                time_aggregation_pipeline,
                ["_id.user", "_id.year", "_id.month", "_id.day", "_id.hour", "_id.category", "activities_sum"],
                ["string:30", "int32", "int32", "int32", "int32", "string:60", "int32"])

1 个答案:

答案 0 :(得分:1)

Monary使用下面的mongoc驱动程序,并且直接以不抽象pymongo驱动程序的方式使用,这实际上是MongoDB公司维护的官方资源。

因此,实现的方式不允许将必要的“选项”传递给aggregate()方法,例如“allowDiskUse”。

你可以看到实现代码here,注意第四和第五个硬编码的参数NULL

// Get an aggregation cursor
mcursor = mongoc_collection_aggregate(collection,
                                      MONGOC_QUERY_NONE,
                                      &pl_bson, NULL, NULL);

当您将此与mongoc_collection_aggregate的文档签名进行比较时,问题就变得清晰了:

mongoc_cursor_t *
mongoc_collection_aggregate (mongoc_collection_t       *collection,
                             mongoc_query_flags_t       flags,
                             const bson_t              *pipeline,
                             const bson_t              *options,
                             const mongoc_read_prefs_t *read_prefs)
   BSON_GNUC_WARN_UNUSED_RESULT;

如果您在处理过程中需要此选项,那么最好直接使用pymongo并根据结果手动加载NumPy数组。

或者,您可以采用reported issue中已经提到过的方法,如果您准备自己构建,请自行修补源代码:

bson_t opts;
bson_init(&opts);
BSON_APPEND_BOOL (&opts, "allowDiskUse", true);
mcursor = mongoc_collection_aggregate(collection,
                                      MONGOC_QUERY_NONE,
                                      &pl_bson, &opts, NULL);
bson_destroy(&opts);

甚至可以自己提供一个完整的补丁,将选项签名添加到方法定义中并正确传递它们。