我可以克隆一个MongoDB聚合管道并聚合更多吗?

时间:2015-06-22 14:36:59

标签: node.js mongodb aggregation-framework

我有很多处理器密集型聚合管道,但唯一的区别实际上是最后几步。

我可以在特定点克隆管道并应用更多聚合以节省冗余部件的时间吗?

E.g。 (警告,这是代码):

collection.aggregate([ // Redundant part of the pipeline
    { $match: query },
    { $project: project },
    { $unwind: unwind },
    { $match: query },
    { $project: project }
], function(err, cursor) { // Unique part of the pipelines

    cursor.aggregate([
        { $group: group1}
    ], cb1);

    cursor.aggregate([
        { $group: group2}
    ], cb2);

    cursor.aggregate([
        { $group: group3}
    ], cb3);
});

我更喜欢使用物理temporary collection在MongoDB适配器中的特定点克隆管道,因为这会在每个Node.js请求上发生,该请求可以同时拥有多个用户,{{1实际上并不意味着临时存储。

1 个答案:

答案 0 :(得分:0)

Redsandro,

您可以通过为" common"创建聚合查询来完成您想要的任务。部分汇总查询。

然后你可以写出"常用步骤"的输出。到了一个新的"使用$ out收集。 http://docs.mongodb.org/manual/reference/operator/aggregation/out/

完成后,您可以将聚合框架查询应用于新创建的集合