Map invoke errors when using MongoDB MapReduce

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.

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.