Database Reference
In-Depth Information
This emit function will return the key and value results the same as a map function would. You can test one using
map.apply() and an example document from your collection as follows:
> map.apply(db.mapreduce.findOne());
emit results - key: blue value: { "num" : 1, "count" : 1 }
Now that you know what to expect out of your map , you can look at debugging your reduce . You first need to
confirm that your map and reduce are returning in the same format— that's critical. The next thing you can do is to
create a short array with a few values just like the ones passed into your reduce , as shown here:
a = [{ "num" : 1, "count" : 1 },{ "num" : 2, "count" : 1 },{ "num" : 3, "count" : 1 }]
Now you can call reduce as follows. This will allow you to see the values returned by your emit:
>reduce("blue",a);
{ "num" : 6, "count" : 3 }
If all else fails and you're confused about what's going on inside your function, don't forget that you can use the
printjson() function to print any JSON value out to the mongodb logfile to read. This is always a valuable tool when
debugging software.
Summary
By now you should have an idea of exactly how much power and flexibility there is within MongoDB, using three of
the most powerful and flexible query systems available. Through your reading of this chapter you should have an
idea of how to use text indexes to perform highly powerful text searches in a number of languages. You should have
the ability to create highly complex and flexible aggregations using the MongoDB aggregation framework. Finally
you should now have the ability to use the powerful JavaScript-backed MapReduce which would allow you to write
powerful groupings and transformations on your data.
 
Search WWH ::




Custom Search