我有以下布局:
{
"URL": "http://someurl.de",
"plugins": {
"HTTPServer": {
"os": [
"FreeBSD"
],
"string": [
"Apache/2.2.21 (FreeBSD) mod_ssl/2.2.21 OpenSSL/0.9.8q DAV/2 PHP/5.3.8 with Suhosin-Patch"
]
}
}
}我想从其中获取存储在plugins.HTTPServer.string中的唯一项的计数。然而,所有的MapReduce示例都只是引用单层文档。根据我对示例的理解,您必须发出map函数中的数据(或选择要提取的数据),然后使用reduce进一步处理结果。我想我的问题是在映射阶段--我需要访问上面的字符串值:"Apache/2.2...“
由于我只在MongoDB度过了最后一天,如果我在这里没有问到正确的问题,请原谅我的无知。我的方向对吗?我知道我可以使用distinct = db.coll.distinct('plugins.HTTPServer.string'),,但是我想用MapReduce来完成这项工作。
map = function() {
server = this.plugins.HTTPServer.string
emit({server : this.server}, {count: 1});
}
reduce = "function(key, values) {
var count = 0;
values.forEach(function(v) {
count += v['count'];
});
return {count: count};
}"发布于 2012-12-04 09:22:45
你有几个问题:
map函数的emit中的
this.server应该只是servermap字段是一个数组,而不是一个单独的字符串,所以你正在发出数组作为你的键,这可能不是你想要的。reduce函数中有零散的"字符。试着这样做:
var map = function() {
if (this.plugins && this.plugins.HTTPServer && this.plugins.HTTPServer.string) {
this.plugins.HTTPServer.string.forEach(function(server) {
emit({server: server}, {count: 1});
});
}
}
var reduce = function(key, values) {
var count = 0;
values.forEach(function(v) {
count += v['count'];
});
return {count: count};
}https://stackoverflow.com/questions/13694168
复制相似问题