M101JS Week 2
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 two of seven. You can read part one 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 topic of the second week is Create, Retrieve, Update, Delete or in short CRUD. This acronym actually is a proper English noun. The dictionary definition of crud is as follows:
- A coating or an incrustation of filth or refuse.
- Something loathsome, despicable, or worthless.
- One who is contemptible or disgusting.
crud. (n.d.) Random House Kernerman Webster’s College Dictionary. (2010). Retrieved August 26 2013 from http://www.thefreedictionary.com/crud
Two of the above definitions could apply to the situation where a software programmer interacts with a database. The first being that the task of writing the code to do CRUD operations is “something despicable or loathsome”. In the second situation the code is written, either by the programmer or some third party, and it's likely an “incrustation of filth or refuse”. In both cases there's a lack of understanding of one or both of the domains application and data storage.
The MongoDB courses are aimed at getting people to understand the basics of interacting with MongoDB. They will show you what crud looks like and how to keep your implementation clean. The second week builds up the basic skills to work with MongoDB. Anyone interested in just poking around in the MongoDB database will be able to do so after this week.
I viewed CRUD lectures before and learned these basics twice. This is why I thought this week to be boring, aside from the Node.js specific instruction material I might not (re)learn much. This made me to at least try all the quizzes. Passing a quiz would give me the confidence to skip the video and focus more on the videos that are new or might be related to something I forgot.
If you watch all the lectures in the order presented, then all the topics of CRUD are first demonstrated using the mongo shell. After you finished all the topics using the mongo shell this knowledge is repeated, but now specifically focused on JavaScript in Node.js.
Each video is short in length, usually between two and five minutes, and following by one quiz question to see if you understood the topic. As each topic usually is a command of the shell or covers a few specific options, going through each video was a drag. The choice for this chopping up of topics will later be much appreciated when you want to reference a particular aspect of CRUD operations. For instance, you want to search inside of arrays. Then you only have to open the lecture on "Using $in and $all", which is roughly 2 minutes and 40 seconds, instead of searching in a lengthy lecture spanning five to ten times that size.
In general the syntax of statements in the Node.js driver matches they syntax of the mongo shell. There are some differences, the most notable being that every function allows a callback function to pass data. This callback function is used to check for errors or access data, because the return value of a function is either a cursor or null.
Operators in query or update commands
The character $ is reserved in MongoDB to indicate that the following word is an operator in either a find or update statement. Depending on what kind of operator you're using they are either the key to an object or a key inside the object. In general you can formulate the statement in a way you would describe it in English. For instance, “update those customers which have a score higher than 90 by incrementing the credit with 10” would become:
[code language="javascript"]
db.customers.update({ 'score': { '$gt': 90 } },
{ '$inc': { 'credit': 10 } },
{ 'multi': true });
[/code]
When you use a logical query operator to combine several selection objects into one it doesn't translate well into English. If you want to update the credit for all customers who haves scores or ages higher than 90, the update statement might look like this:
[code language="javascript"]
db.customers.update({ '$or': [ { 'score': { '$gt': 90 } },
{ 'age': { '$gt': 90 } } ] },
{ '$inc': { 'credit': 10 } },
{ 'multi': true });
[/code]
Homework
The second week had three homework assignments. The first a mongo shell assignment, the second a Mongo driver assignment and the last was related to the blog project of the course. The blog project is used in all M101 courses, since those are for programmers and a project is a nice way to see progress.
This week I also had the first encounter with the asynchronous nature of the driver. In short, I forgot to keep the database connection open until I performed my task in homework assignment two. I am embarrassed that it took me almost four hours to realize this. This might actually be the hardest part of the course, knowing when to close the database connection. I don't know yet if the driver cleans up connections automatically, but either way it's not good to keep resources open when an exception occurs. Actually it's not good to ignore errors.
In week three the course delves into schema design. I wonder how Node.js specific content can be incorporated into valuable lessons, because schema design doesn't have many language specific facets. Once I completed that week, I will report again.
- ← Previous
M101JS Week 1 - Next →
M101JS Week 3