我想要一个map reduce函数从下面的输入集合中绘制下面的输出,满足以下条件。
输入集合:
[{
a:1,
b:'test',
indices:[1,2,4,5]
}, {
a:2,
b:'test',
indices:[2, 3, 5]
}, {
a:2,
b:'test',
indices:[1, 2, 4]
}, {
a:3,
b:'apple',
indices:[1, 2]
}, {
a:4,
b:'apple',
indices:[1, 3, 5]
}, {
a:5,
b:'orange',
indices:[232]
}, {
a:5,
b:'dummy',
indices:[2]
}, {
a:6,
b:'dummy',
indices:[11, 2, 4]
}, {
a:6,
b:'dummy',
indices:[11, 3, 2]
}, {
a:6,
b:'dummy',
indices:[1, 2, 3, 4, 5]
}]
条件是:
2
的内容。这可以发送为
查询。即查询:{indices:{$ in:2}} b
a
,则应将其视为1
,例如:满足条件索引的两次出现a = 2的文档
有2。dummy/apple/etc
中。但是
可以重复。以下是我的尝试:
db.x.mapReduce(function(){
emit(this.b, 1);
}, function(key, reducable){
return Array.sum(reducable);
}, {
out: {inline: 1},
query:{
'indices':{$in:2}
}
});
输出: [
{
"_id" : test",
"value" : {
"count" : 3 -> It should be 2
}
},{
"_id" : apple",
"value" : {
"count" : 2
}
},{
"_id" : dummy",
"value" : {
"count" : 4 -> It should be 2
}
}]
预期产出:
[{
"_id" : test",
"value" : {
"count" : 2
}
},{
"_id" : apple",
"value" : {
"count" : 2
}
},{
"_id" : dummy",
"value" : {
"count" : 2
}
}]
答案 0 :(得分:0)
无需map / reduce。使用聚合:
> db.crawler_status.aggregate([
{ "$match" : { "indices" : 2 } },
{ "$group" : { "_id" : { "b" : "$b", "a" : "$a" } } },
{ "$group" : { "_id" : "$_id.b", "count" : { "$sum" : 1 } } }
])
{ "_id" : "test", "count" : 2 }
{ "_id" : "apple", "count" : 1 } // your sample output was mistaken
{ "_id" : "dummy", "count" : 2 }