【问题标题】:How to return a temporary collection in the callback of a mongodb find() using node-mongodb-native?如何使用 node-mongodb-native 在 mongodb find() 的回调中返回临时集合?
【发布时间】:2012-05-15 13:41:16
【问题描述】:

有没有办法从 find() 操作而不是游标返回临时集合?我正在使用 node-mongodb-native 驱动程序,我想对 find() 操作的结果运行 mapReduce。像这样的:

client.open(function(err) {
    client.collection(collectionName, function(err, collection) {
        collection.find( {days_since_epoch: {$gte: query.begin}).toArray(function(err, docs) {
            // Need to run a mapReduce here on the results
            // Preferably on a temporary collection instead of the default cursor or array above
       });
    });
});

可能有更好的方法来做到这一点,所以我也愿意。谢谢。

【问题讨论】:

    标签: javascript node.js mongodb


    【解决方案1】:

    不,但是 map-reduce 函数需要一个查询,所以你不需要 find :)

    http://mongodb.github.com/node-mongodb-native/api-generated/collection.html#mapreduce

    【讨论】:

    • 谢谢。我不清楚的是该查询是应用于 map 还是 reduce 函数。我猜它必须是 map 函数,因为当输出传递给 reduce 函数时,原始键将不存在。对吗?
    • 是的,它基本上只是一个过滤器,如果您正在执行增量地图缩减,您可能会根据日期或增量计数器选择时间序列并存储每次您的位置运行 map-reduce。
    • 2.2 中的聚合框架也值得一看,因为它可能能够将大多数组/订单 map-reduce 用于mongodb.org/display/DOCS/Aggregation+Framework
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    相关资源
    最近更新 更多