I have the following layout:
{
"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"
]
}
}
}
From which I want to get the count of the unique items stored in plugins.HTTPServer.string. All the MapReduce examples however just refer to single level documents. As I understand the examples, you have to emit the data in the map function (or choose data you want to extract) and then reduce is used to further process the results. I think my problem is during the mapping stage – that I need to get access to the string value above that reads: “Apache/2.2…”
As I’ve only spent the last day in MongoDB, forgive my ignorance if I’m not asking the right questions here. Am I headed in the right direction? I know I can use distinct = db.coll.distinct(‘plugins.HTTPServer.string’), but I’d like to get this done with 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};
}"
You’ve got a few problems:
this.serverin themapfuntion’s emit should just beserver"string"field is an array, not a single string, so you’re emitting the array as your key which probably isn’t what you want."characters in yourreducefunction.Try this instead: