2011-01-05 22:05:10 -08:00
|
|
|
|
|
|
|
|
t = db.mr_replace;
|
|
|
|
|
t.drop();
|
|
|
|
|
|
2016-03-09 12:17:50 -05:00
|
|
|
t.insert({a: [1, 2]});
|
|
|
|
|
t.insert({a: [2, 3]});
|
|
|
|
|
t.insert({a: [3, 4]});
|
2011-01-05 22:05:10 -08:00
|
|
|
|
|
|
|
|
outCollStr = "mr_replace_col";
|
|
|
|
|
outDbStr = "mr_db";
|
|
|
|
|
|
2016-03-09 12:17:50 -05:00
|
|
|
m = function() {
|
|
|
|
|
for (i = 0; i < this.a.length; i++)
|
|
|
|
|
emit(this.a[i], 1);
|
|
|
|
|
};
|
|
|
|
|
r = function(k, vs) {
|
|
|
|
|
return Array.sum(vs);
|
|
|
|
|
};
|
2011-01-05 22:05:10 -08:00
|
|
|
|
2016-03-09 12:17:50 -05:00
|
|
|
function tos(o) {
|
2011-01-05 22:05:10 -08:00
|
|
|
var s = "";
|
2016-03-09 12:17:50 -05:00
|
|
|
for (var i = 0; i < 100; i++) {
|
|
|
|
|
if (o[i])
|
2011-01-05 22:05:10 -08:00
|
|
|
s += i + "_" + o[i];
|
|
|
|
|
}
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 12:29:01 -05:00
|
|
|
print("Testing mr replace into other DB");
|
2016-03-09 12:17:50 -05:00
|
|
|
res = t.mapReduce(m, r, {out: {replace: outCollStr, db: outDbStr}});
|
|
|
|
|
printjson(res);
|
|
|
|
|
expected = {
|
|
|
|
|
"1": 1,
|
|
|
|
|
"2": 2,
|
|
|
|
|
"3": 2,
|
|
|
|
|
"4": 1
|
|
|
|
|
};
|
2011-01-05 22:05:10 -08:00
|
|
|
outDb = db.getMongo().getDB(outDbStr);
|
|
|
|
|
outColl = outDb[outCollStr];
|
2016-03-09 12:17:50 -05:00
|
|
|
str = tos(outColl.convertToSingleObject("value"));
|
2011-01-05 22:05:10 -08:00
|
|
|
print("Received result: " + str);
|
2016-03-09 12:17:50 -05:00
|
|
|
assert.eq(tos(expected), str, "A Received wrong result " + str);
|
2011-01-05 22:05:10 -08:00
|
|
|
|
|
|
|
|
print("checking result field");
|
2016-02-04 12:29:01 -05:00
|
|
|
assert.eq(res.result.collection, outCollStr, "B1 Wrong collection " + res.result.collection);
|
|
|
|
|
assert.eq(res.result.db, outDbStr, "B2 Wrong db " + res.result.db);
|
2011-01-05 22:05:10 -08:00
|
|
|
|
|
|
|
|
print("Replace again and check");
|
2016-03-09 12:17:50 -05:00
|
|
|
outColl.save({_id: "5", value: 1});
|
|
|
|
|
t.mapReduce(m, r, {out: {replace: outCollStr, db: outDbStr}});
|
|
|
|
|
str = tos(outColl.convertToSingleObject("value"));
|
2011-01-05 22:05:10 -08:00
|
|
|
print("Received result: " + str);
|
2016-03-09 12:17:50 -05:00
|
|
|
assert.eq(tos(expected), str, "C1 Received wrong result " + str);
|