Filtering the input collection

Sometimes, we don't need the entire collection for our MapReduce process. Because of this, the mapReduce command provides us with optional parameters to filter the input collection.

The query parameter allows us to apply criteria using the query operators to filter the documents input into the map function. In the following code, we will filter the documents in the collection and only include the documents where the number attribute is greater than 10 ("$gt":10):

collection.map_reduce(map_function,                         
                         reduce_function,                      
                      "output_collection",                            
                            query={"number":{"$gt":10}})

The Query Operators used in the MapReduce query parameter are the same query selectors seen in Chapter 12, Data Processing and Aggregation with MongoDB ...

Get Practical Data Analysis - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.