Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Following is a document example in posts collection {\"post_text\", \"tutorialsp

ID: 3817253 • Letter: F

Question

Following is a document example in posts collection {"post_text", "tutorialspoint is an awesome website for tutorials", "user_name"; mark", "status"; "active"} Fill the blanks to use a m function to select all the active posts, group them on the basis of user_name and then count the of posts by each user. Results will be saved in to number post_total collection. db.______._______(function() {emit(______, 1);}, function(______, ______) {return Array.______ (values)}, {query: ?{______}, ______: "post_total"})

Explanation / Answer

This is a question of MongoDB.

The function is to be written on the 'posts' collection.
Now here, you are supposed to write the mapReduce function for selecting all the posts having status is 'active'.

So the query is status:"active".

Now, the resulted posts are to be grouped by user name and counted the number for every user.

So, once we get the key value pairs grouped by user, we can simply apply the sum() function to get the count for those pairs.

So, emitting the result using user id gives the desired result : grouping by user.

The last thing to do is : Store the result in the collection post_total. This is achieved using 'out'.

So, the complete function can be written as follows:

db.posts.mapReduce(
function() { emit(this.user_id,1); },
  
function(key, values) {return Array.sum(values)}, {
query:{status:"active"},
out:"post_total"
}
)

You have posted only one sample document. So, the result can not be displayed without the complete collection information. But this is the required query.

Do comment if there is any query. Thank you. :)