Mongodb-Map-Reduce-不返回完整数据 [英] Mongodb - Map-Reduce - Complete data is not returned

查看:98
本文介绍了Mongodb-Map-Reduce-不返回完整数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Map Reduce.问题是,如果没有.输入的文档> 100,那么我没有得到预期的编号.结果,但如果没有.输入文档的数量是< == 100,那么我得到的结果是预期的.

I am using Map Reduce. The problem is that if the no. input of documents is > 100, then I am not getting the expected no. of results but if the no. of input documents is <= 100, then I am getting the results as expected.

我得到的示例输出:

{
    "_id" : "5504",
    "value" : [
            ObjectId("51c921bae4b0f0f776b339d2"),
            ObjectId("51b06b5be4b021e44bc69755")
    ]
}

问题::如果有< = 100个用户文档(id:5504),那么我得到的则是很多.输出数组中的ID的数量,但如果没有.文档> 100,那么我在输出数组中得到的ID很少.当没有时我得到了上面的输出.此用户的文档数是101,但是当它是100时,我得到了100个ID.为什么会有这种奇怪的行为?对此有什么解决方案?

Problem: If there are <= 100 documents for user (id:5504), then I am getting that many no. of ids in the output array but if the no. of documents >100, then I am getting very few ids in the output array. I got the above output when the no. of documents for this user was 101, but when it was 100, I got 100 ids. Why this strange behaviour and what's the solution for this?

地图功能:

db.system.js.save({

    _id: "map1",

    value: function () {
        var value = {
            "data": [{
                "_id": this._id,
                "creation_time": this.creation_time
            }]
        };
        emit(this.user_id, value);
    }
});

减少功能:

db.system.js.save({

    _id: "reduce1",

    value: function (key, values) {
        var reducedValue = [];
        for (var i = 0; i < values.length; i++) {
            reducedValue.push({
                "_id": values[i].data[0]._id,
                "creation_time": values[i].data[0].creation_time
            });
        }
        return {
            data: reducedValue
        };
    }
});

最终确定功能:

db.system.js.save({

    _id: "finalize1",

    value: function (key, reducedValue) {
        var a = reducedValue.data.sort(compare1);
        var ids = [];
        for (var i = 0; i < a.length; i++) {
            ids.push(a[i]._id);
        }
        return ids;
    }
});

比较功能:

db.system.js.save({

    _id: "compare1",

    value: function (a, b) {
        if (a.creation_time < b.creation_time) return 1;
        if (a.creation_time > b.creation_time) return -1;
        return 0;
    }
});

MapReduce()调用

db.notifications.mapReduce(map1, reduce1, {out: "notifications_result", query: {delivered:true, user_id:"5504"}, finalize: finalize1});

推荐答案

由于MongoDB可以多次调用reduce函数,因此必须确保

Since MongoDB could call reduce function many times, you must ensure Function Idempotence. A little modification on your reduce function solves the problem:

db.system.js.save({

    _id: "reduce1",

    value: function (key, values) {
        var reducedValue = [];
        for (var i = 0; i < values.length; i++) {
            for(var j = 0; j < values[i].data.length; j++) {
                reducedValue.push({
                    "_id": values[i].data[j]._id,
                    "creation_time": values[i].data[j].creation_time
                });   
            }

        }
        return {
            data: reducedValue
        };
    }
});

请注意,现在也遍历了values[i].data数组,因为其他reduce1的返回调用了

Note that now the values[i].dataarray is traversed too, because the return of other reduce1 calls are in the values array.

这篇关于Mongodb-Map-Reduce-不返回完整数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆