Вот карта / уменьшенная версия:
db.data.save({
"name" : "item1",
"m" : 5.2,
"v" : 1.1,
"data1" : [ 0, 0, 0.3, 0.7, 0.95, 0.9, 0.75, 0.4, 0.1, 0 ],
"data2" : [ 0, -1, 0, 1, 1, 0 ]
});
db.data.mapReduce( function() {
var searchdoc = {
"x" : 8,
"K" : 1,
"dataA" : [ 0, 0, 0, 0, 0, 0, 0, 0.5, 1, 0.5],
"dataB" : [ 0, -2, 0, 1, 1, 0 ]
};
var result = {name: this.name};
result.y = searchdoc.K * this.v * Math.pow(searchdoc.x, 1 / this.m);
if(result.y > 0) {
result.dataF = 0;
for(i=0;i<this.data1.length;i++) {
var f = this.data1[i] * searchdoc.dataA[i];
if(f > result.dataF) {
result.dataF = f;
}
}
result.dataS = 0;
for(i=0;i<this.data2.length;i++) {
var s = this.data2[i] * searchdoc.dataB[i];
result.dataS += s;
}
emit(this.name, result);
}
}, function(key, values){}, {out: {inline: 1}});
результат:
{
"results" : [
{
"_id" : "item1",
"value" : {
"name" : "item1",
"y" : 1.640830939540542,
"dataF" : 0.2,
"dataS" : 4
}
}
],
"timeMillis" : 0,
"counts" : {
"input" : 1,
"emit" : 1,
"reduce" : 0,
"output" : 1
},
"ok" : 1,
}
Это версия оболочки:
db.data.save({
"name" : "item1",
"m" : 5.2,
"v" : 1.1,
"data1" : [ 0, 0, 0.3, 0.7, 0.95, 0.9, 0.75, 0.4, 0.1, 0 ],
"data2" : [ 0, -1, 0, 1, 1, 0 ]
});
var searchdoc = {
"x" : 8,
"K" : 1,
"dataA" : [ 0, 0, 0, 0, 0, 0, 0, 0.5, 1, 0.5],
"dataB" : [ 0, -2, 0, 1, 1, 0 ]
};
var search = function(searchdoc) {
db.data.find().forEach(function(obj) {
var result = {name:obj.name};
result.y = searchdoc.K * obj.v * Math.pow(searchdoc.x, 1 / obj.m);
if( result.y > 0 ) {
result.dataF = 0;
for(i=0;i<obj.data1.length;i++) {
var f = obj.data1[i] * searchdoc.dataA[i];
if(f > result.dataF) {
result.dataF = f;
}
}
result.dataS = 0;
for(i=0;i<obj.data2.length;i++) {
var s = obj.data2[i] * searchdoc.dataB[i];
result.dataS += s;
}
db.results.save(result);
}
});
}
search(searchdoc);
db.results.find();
{ "_id" : ObjectId("4f08ffe4264d23670eeaaadf"), "name" : "item1", "y" : 1.640830939540542, "dataF" : 0.2, "dataS" : 4 }