mongodb - 使用mapreduce处理逻辑规则?

时间:2012-02-10 19:27:41

标签: mongodb logic mapreduce rules

假设我有这三条规则:

[{ 
    selector: {_id: "1"},
    value: {name: "apple"} 
},
{ 
    selector: {name: "apple"},
    value: {type: "fruit"} 
},
{ 
    selector: {type: "fruit"},
    value: {class: "food"} 
}]

我想最终得到这个结果:

{
    _id: 1,
    value: {
        name: "apple",
        type: "fruit",
        class: "food"
    }
}

我能用mapreduce做这件事吗?

2 个答案:

答案 0 :(得分:0)

这是我对问题的看法(带有mongodb驱动程序的node.js)。我很确定它可以进行一些优化。

var logicize = function(db, callback) {
    var reduce = function(key, values){
        // If values is an array of objects, it is merged into a single object
        if(values.length) {
            while(values.length>1) {
                var current = values.pop();
                for (var property in current) {
                    values[0][property] = current[property];
                }
            }
            return values[0];
        }
        return values;
    };

    db.collection("rules", function(err, rules) {
        db.collection("results", function(err, results) {
            rules.mapReduce("function() {emit(this.selector._id, this.value);}", reduce, {out: {replace:"results"}, query:{"selector._id":{$exists:true}}}, function() {
                rules.find({"selector._id":{$exists:false}}, function(err, cursor) {            
                    cursor.nextObject(function(err, item) { 
                        // Recursive because I don't want to start a new mapreduce 
                        // before the previous one has finished. The following one 
                        // might depend on the results of the previous
                        (function recurse(item) {
                            if(item==null) // Done
                                callback();
                            else {
                                var map = new Function('emit(this._id, '+JSON.stringify(item.value)+');');

                                var conditions = {};
                                for(var condition in item.selector) {
                                    conditions['value.'+condition] = item.selector[condition];
                                }
                                results.mapReduce(map, reduce, {out:{reduce:"results"},query: conditions}, function() { 
                                    // Previous mapreduce has finished so we can start the next one
                                    cursor.nextObject(function(err, item) {
                                        recurse(item);
                                    });
                                });
                            }
                        })(item);
                    });
                });
            });
        });
    });
}

规则在“规则”集合中,结果进入“结果”。我首先使用具有_id的规则执行初始mapreduce。在那之后,我为每个其他规则运行一个单独的mapreduce。

答案 1 :(得分:0)

你可以用mapreduce做任何你想做的事情,因为你可以在你的所有对象上执行任意的javascript。但是你应该知道mapreduce现在不是很快,建议不要在map / reduce函数中执行“db”操作(与分片不一致并且可能会产生锁定问题)。

理想情况下,第一步是尝试对数据进行建模,使其预先填充,即使这意味着要复制某些数据。如果那是不可能的,你应该比较mapreduce与工作客户端的速度。