これは私の問題です(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を持つルールだけで初期のマップリダクションを実行しています。その後、別のルールごとにマップリダクションを実行しています。
私はあなたがreduceでそれを行うことができると確信しています。 –
は、あなたがしようとしていることですか? mongodbは、非正規化されたデータを格納するという考え方のもとに構築されているので、データを間違った方法で並べ替えることができます。 – Kevin
この場合、結合は機能しますが、任意の長い複雑なオブジェクトのリスト規則のリスト。これは、より複雑なユースケースを単純化したものです。 –