내 문제는 여기에 있습니다 (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를 수행하고 있습니다. 그 후에 다른 규칙마다 별도의지도 축소를 실행합니다.
나는 줄일 수 있다고 확신한다. –
이 조인처럼 보입니다. mongodb는 비정규 화 된 데이터를 저장한다는 아이디어를 바탕으로 만들어 졌으므로 잘못된 방식으로 데이터를 정렬 할 수 있습니다 ... – Kevin
이 경우 조인이 가능하지만 임의로 길고 복잡한 객체 목록을 생성하는 데 관심이 있습니다. 규칙 목록. 이것은보다 복잡한 유스 케이스의 총체적인 단순화이다. –