db.things.find()
{ "_id" : 1, "tags" : [ "dog", "cat" ] }
{ "_id" : 2, "tags" : [ "cat" ] }
{ "_id" : 3, "tags" : [ "mouse", "cat", "dog" ] }
m = function () {
this.tags.forEach(function (z) {
//统计前过滤
if(z == 'dog'){
emit(z, {count:1});
}
});
}
f=function (key, values) {
var result = {count:0};
values.forEach(function (value) {result.count += value.count;});
return result;
}
m = function () {
emit(actionType, {count:1});
}
f=function (key, values) {
var result = {count:0};
values.forEach(function (value) {result.count += value.count;});
return result;
}
res = db.things.mapReduce(m, f, {out: {replace: "test_result"}});
http://www.faithbee.com/?p=180
{ "_id" : 1, "tags" : [ "dog", "cat" ] }
{ "_id" : 2, "tags" : [ "cat" ] }
{ "_id" : 3, "tags" : [ "mouse", "cat", "dog" ] }
m = function () {
this.tags.forEach(function (z) {
//统计前过滤
if(z == 'dog'){
emit(z, {count:1});
}
});
}
f=function (key, values) {
var result = {count:0};
values.forEach(function (value) {result.count += value.count;});
return result;
}
m = function () {
emit(actionType, {count:1});
}
f=function (key, values) {
var result = {count:0};
values.forEach(function (value) {result.count += value.count;});
return result;
}
res = db.things.mapReduce(m, f, {out: {replace: "test_result"}});
http://www.faithbee.com/?p=180