【发布时间】:2011-09-06 13:31:00
【问题描述】:
我有一个条目看起来像这样的集合:
{"userid": 1, "contents": [ { "tag": "whatever", "value": 100 }, {"tag": "whatever2", "value": 110 } ] }
我正在使用 {"contents.tag": "whatever"} 等查询对该集合执行 MapReduce。
我想在我的 map 函数中做的是发出与数组“contents”中匹配查询的条目对应的字段“value”无需遍历整个数组 .在正常情况下,我可以使用带有 content.$.value 之类的 $ 位置运算符来做到这一点。但在 MapReduce 案例中,它不起作用。
总而言之,这是我现在拥有的代码:`
map=function(){
emit(this.userid, WHAT DO I WRITE HERE TO EMIT THE VALUE I WANT ?);
}
reduce=function(key,values){
return values[0]; //this reduce function does not make sense, just for the example
}
res=db.runCommand(
{
"mapreduce": "collection",
"query": {'contents.tag':'whatever'},
"map": map,
"reduce": reduce,
"out": "test_mr"
}
);`
有什么想法吗?
谢谢!
【问题讨论】: