Skip to main content
Glimpses of Daniel's world

M101JS Week 5

On August 12th the first M101JS (MongoDB for Node.js developers) course started. Previously I completed both the M101J (MongoDB for Java developers) and M102 (MongoDB for DBAs) courses available through 10gen's education site.

When I finish this course I will have refreshed my MongoDB knowledge and got some experience with Node.js in the form of a blog application. I decided to document my progress in (at least) weekly summaries. This will be part five of seven. You can read part four here.

For this series of blog posts I will structure each post into an introduction (you just passed it) and two sections. The first section, Lectures, will summarize what I learned from the videos and related examples. In the second section, Homework, I will mention what I learned from practicing the homework assignments and anything I might have done extra because of it.

Lectures

The fifth week covered the aggregation framework of MongoDB. The drivers map to the aggregation framework in a very similar way to what the mongo shell does, therefore there was no Node.js specific material in this week's lectures.

I already was familiar of the way piping in unix environments is used as an analogy when explaining the aggregation pipeline. In order to use this pipeline you execute the aggregate command on a collection. It's parameter is an array of operations that is on all the data you can pull from that one collection. MongoDB has other aggregation commands, the single purpose aggregation commands and the Map-Reduce command but these weren't mentioned.

Working through the videos and quizzes was tedious at times, but I did it anyway just to make sure I understood what was happening or just needed to refresh my knowledge. There wasn't much to learn, because I still remembered most of the commands and the syntax doesn't deviate much from anything you would know by now. Which means the most interested part of the week was actually doing the homework assignments.

Homework

This week had four assignments. Two deal with data on ZIP codes, one of the free data sets available on the MongoDB documentation site, one concerns to the blog project and for the last assignment you had to aggregate data on student scores.

The most important thing was to read the assignment properly. Each assignment had an example of what different input should give as a result or the opposite result of what was asked. It was a good way to verify your result before selecting the right multiple choice option.

Got stuck

Getting through these homework assignments should have been simple, but sometimes I got stuck. This happened because the unwind operator's syntax threw me off. The assignment was to get the average score for each class based on a supply of student scores. There was however one requirement, quiz scores must be excluded. Because the scores are in an array, you need to unwind this array and then filter the resulting documents properly. In my first approaches I got some syntax errors and when I cleared those up my pipeline was somewhat like below:

[code language="javascript"]
{'$unwind': '$scores'},
{'$match': { '$scores.type' : { '$ne' : 'quiz' } } }
[/code]

This was wrong, because it didn't filter the quizzes out at all. Then I realized my mistake. This section should have been as below:

[code language="javascript"]
{'$unwind': '$scores'},
{'$match': { 'scores.type' : { '$ne' : 'quiz' } } }
[/code]

The difference is just a dollar sign, but a very crucial difference. With the dollar sign in place the match would be against a field in the collection whose key would be the type of score. If a document would show the score of an exam, then the match would be on the field exam, which doesn't exist, therefore the condition returns true and the document stays available for further processing.

I wonder if there is an actual use for this feature, or if a collection where such a thing is possible actually proves that the design of the collection is bad. Perhaps I can do some research on it and write a blog.

Using Robomongo

During this week I decided to use some sort of GUI to write and save my aggregation framework attempts and the choice fell on Robomongo. First of all because it seemed up to date and second that it is cross-platform which means I can still use it in the future if I ever decide to switch platforms (again).

Aside from doing some syntax highlighting, it can show the results in three different ways. Another feature I liked is that I can get multiple result sets if I put several queries in the shell window. What I liked most was the ability to save my scripts. This and auto-completion made me prefer it over the plain old shell, which of course can be customized to do syntax highlighting.

Next week the lecture will be about Application Engineering. According to the syllabus the lectures are about drivers and implications of sharding and replication on the design. I wonder how deep the lectures will go into these subjects.