Filtering the input collection
Sometimes, we don't need the entire collection for our MapReduce process. Hence, the mapReduce
command provides us with optional parameters to filter the input collection.
The parameter query
allows us to apply criteria using the query
operators to filter the document's input to the map
function. In the following code, we will filter the documents in the collection, and only include the documents where the attribute number
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 the Getting started with MongoDB section in Chapter 12, Data Processing and Aggregation with MongoDB, used to perform simple queries. In the following table we present the most common operators and their equivalent in SQL language:
Mongo operators |
SQL operators |
---|---|
|
|
|
|
|
|