Node.js and MongoDB: Part 1
“If you aren’t going forwards, you’re going backwards” the old adage goes. I recently picked up this book called Node.JS, MongoDB and Angular JS Web Development. I normally use PHP MVC, SQL Server and HTML5 for coding my web applications in a personal or work environment but I figured it is best to pick up on some of the most widely used languages that other companies use as well to help round out my technology education. I am familiar with Node.js from trying to create my own artificially intelligent chatbot a few years back using Wit.AI. However MongoDB and AngularJS I have heard about, but never actually done anything with them.
Learning MongoDB has been interesting, it is nothing like SQL Server or MySQL which I have used many times before. I have successfully launched the MongoDB daemon, created a user and database admin and turned on authentication for the database.
The next section of this book starts discussing how to connect Node.js to MongoDB, however at the time the book was published, they must have used an older version of the MongoClient but never listed the version they use in this book. Therefore much of the code does not actually work and I was left to search online with the official MongoDB API Documentation to try and figure out how to make a connection and perform operations. I’m not entirely mad about this, I am still learning the ropes, but it definitely can be an obstacle to some who just buy the book hoping its a one stop shop and all of a sudden nothing is working for them (and many people actually say that in the reviews I have seen online for the book).
It definitely does present more of a challenge to not be able to word for word use the books source code but to look at the book and then compare to official documentation and refactor the code as needed to make it work. Once I begin to hit chapter 13 dealing with all the Node.JS and MongoClient code, a lot of the books code was deprecated so I needed to refactor a lot of it on my own for it to work. With VS Code, I can get tooltips that guide me with the official documentation, which is SUPER helpful:
Starting in Chapter 14, I learned how to do a whole bunch of things to documents in a MongoDB collection such as: creating, inserting, saving, updating.etc. The book makes a separate file for each of these methods, I decided to make one giant document.js and you can pass a parameter such as ‘save’ or ‘update’ and it uses the correct method to do so. Plus the code is refactored up to the current 3.2 version.
I have this chapter and all of the code for my practice in my GitHub at this repo. At this point, I have created one rather lengthy blog post since Chapter 14 was quite a long chapter so I will stop this post here and continue Chapter 15 in the next post!