3

I am trying to make a simple map/reduce function on one of my MongoDB database collections. I get data but it looks wrong. I am unsure about the Map part. Can I use IF/ELSE in this way?

UPDATE

I want to get the amount of authors that ownes the files. In other words how many of the authors own the uploaded files and thus, how many authors has no files.

The objects in the collection looks like this:

{
    "_id": {
        "$id": "4fa8efe33a34a40e52800083d"
    },
    "file": {
        "author": "john",
        "type": "mobile",
        "status": "ready"
    }
}

The map / reduce looks like this:

$map = new MongoCode ("function() {

if (this.file.type != 'mobile' && this.file.status == 'ready') {

 if (!this.file.author) {

  return;

 }

 emit (this.file.author, 1);

}

}");

$reduce = new MongoCode ("function( key , values) {

 var count = 0;

 for (index in values) {

  count += values[index];

 }

 return count;

}");

$this->cimongo->command (array (

 "mapreduce" => "files",  

 "map"       => $map,   

 "reduce"    => $reduce,  

 "out"       => "statistics.photographer_count"

)

);
Jonathan Clark
  • 19,726
  • 29
  • 111
  • 175
  • what's the purpose of the !this.file.author check? do you have some documents which are mobile and ready but don't have the author set? – Asya Kamsky Jul 21 '12 at 19:04

2 Answers2

1

The map part looks ok to me. I would slightly change the reduce part.

values.forEach(function(v) {
  count += v;
}

You should not use for in loop to iterate an array, it was not meant to do this. It is for enumerating object's properties. Here is more detailed explanation.

Why do you think your data is wrong? What's your source data? What do you get? What do you expect to get?

Community
  • 1
  • 1
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
1

I just tried your map and reduce in mongo shell and got correct (reasonable looking) results.

The other way you can do what you are doing is get rid of the inner "if" condition in the map but call your mapreduce function with appropriate query clause, for example:

db.files.mapreduce(map,reduce,{out:'outcollection', query:{"file.author":{$exists:true}}})

or if you happen to have indexes to make the query efficient, just get rid of all ifs and run mapreduce with query:{"file.author":{$exists:true},"file.type":"mobile","file.status":"ready"} clause. Change the conditions to match the actual cases you want to sum up over.

In 2.2 (upcoming version available today as rc0) you can use the aggregation framework for this type of query rather than writing map/reduce functions, hopefully that will simplify things somewhat.

Asya Kamsky
  • 41,784
  • 5
  • 109
  • 133