Time for action — writing the map function to calculate ratings

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});
});
}

What just happened?

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}.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.131.160.69