This is how we can write the map function. As we have seen earlier, this function will emit information, in our case, the key is the username and the value is the rating:
function() { this.votes.forEach(function(x) { emit(x.username, {rating: x.rating}); }); }
This is a JavaScript function. MongoDB understands and processes all JS functions. Every time emit()
is called, some data is emitted for the reduce function to process. In the preceding code this
represents the collection object.
What we want to do is emit all the ratings for each element in the votes array for every book. The emit()
takes the key
and value
as parameters. So, we are emitting the users votes for the reduce function to process. It's also important to remember the data structure we are emitting as the value. It should be consistent for all objects. In our case {rating: x.rating}
.
3.131.160.69