When running MapReduce operations on your data, you must make sure that any fields you refer to within your map operation are available for every document in your collection. If you try a map operation and some documents do not have the required field, you will get the following assertion error when running the command:
map invoke failed: JS Error: TypeError: this.fieldname has no properties
fieldname
in this case is a field that does not exist in all of the documents.
To prevent this error, you can pass the optional query
parameter to the command to make sure that only documents with this field are queried. For example, in PHP, add the following to the command operation:
$db->command( 'mapreduce' => 'collection', 'map' => $map, 'reduce' => $reduce, 'out' => array( 'inline' => 1 ), 'query' => array( "fieldname" => array( '$exists' => true, ) ) );
This makes sure that MapReduce operations are only run on a subset of the collection.