JS Questions: What is MongoDB?

This is the fourteenth in a series of posts I’m writing that aims to provide concise, understandable answers to JavaScript-related questions, helping me better understand both foundational concepts and the changing landscape of language updates, frameworks, testing tools, etc.

What is MongoDB?

MongoDB is an open-source database program. There are several database management systems; some of the most popular are Oracle, MySQL, and PostgreSQL. MongoDB has been gaining popularity in recent years and is different from the ones above, because it’s a NoSQL, or non-relational, database management system.

First, Relational Databases…

Relational databases have rows of data stored in tables. A row of data can contain columns with keys that link one record to another. For instance, if there were a relational database containing data about the show Comedians in Cars Getting Coffee, it may have a table for episodes and another table for comedians. Every episode features one comedian (aside from the comedian host, Jerry Seinfeld) so each episode record could have a column called comedian_id that contains a key corresponding with a comedian from the comedian table.

Below is a visual showing how the data would be stored. The only episode in the episodes table has a comedian_id of 2. Kristen Wiig is the comedian in the comedians table with the primary key 2, so she is the comedian for this episode.

Screen Shot 2018-01-22 at 10.47.32 AM.png

Using the database querying language, SQL, tables can reference each other’s data through JOIN statements. Here’s an example:

SELECT Episodes.Title, Comedians.Name FROM Episodes INNER JOIN Comedians ON Comedian.Id = Episodes.Comedian_id;

This is saying: show episode titles with their corresponding comedian names by using the episode’s comedian_id to find the comedian with an id of the same number.

Now, NoSQL Databases…

Records are stored differently with MongoDB. Instead of table rows, data is stored in JSONlike documents.

A document can contain nested information. For instance, an episode document may contain a nested comedian document. Or, a document for the show’s seasons could have a nested array of episodes.

Below is an example of what a document might look like for an episode with one comedian.

mongo_ex.png

A grouping of documents–all comedians, for example–is called a collection. It’s the equivalent to a table in a relational database, though unlike a relational database, items do not need to have the same schema.

The mongo shell can be used to interact with a database and perform actions, such as retrieving all documents from a collection. The below code would grab all comedians from a database (db) with a comedians collection, using the .find() operation:

db.comedians.find()

Since documents can store nested data (such as an episode’s comedian), the need to perform a costly ‘join’ in order to access data is reduced. (MongoDB does have the functionality to join collections when needed.)

MongoDB in context…

MongoDB is generally associated with the latest wave of web applications and tech startups. Of course, different database management systems are suitable for different projects, but these are a few reasons (I think) MongoDB is gaining momentum.

MongoDB is well suited to the Agile style of software development that has become popular. Engineers using a quick, iterative development approach can easily change their schema and models as needed.

For startups and fast growing applications, there’s also the benefit of MongoDB’s distributed database design. Data is stored across multiple machines by default, so the database can easily expand as needed.

The 2017 Stack Overflow Developer Survey has a correlated technologies graph showing MongoDB closely linked with the frameworks Angular, Node.js, React, and Cordova, as well as the languages, JavaScript and TypeScript (a syntactical superset of JavaScript). While it’s not necessary to pair MongoDB and JavaScript (there are several ‘drivers‘ that can be used to interact with MongoDB with different programming languages), building full stack applications entirely with JavaScript is growing in popularity. Two common stacks for building modern web applications are MEAN (MongoDB, Express, Angular, Node.js) and MERN (MongoDB, Express, React, Node.js).

Part of the excitement about MongoDB can also be attributed to the fact that it’s new, has an excited community, growing features, and pretty approachable documentation and educational features on the MongoDB site.

Final note: There’s a lot of debate about whether MongoDB is better than SQL options. MongoDB may be faster in some cases (such as the previous example of reading an episode’s comedian), but SQL databases have their own advantages, including the reliability of a mature system (Oracle has been around since 1977). This Sitepoint article offers an introduction to best use-cases for SQL and NoSQL databases.

Resources

 

Advertisements

Ruby: Using Directories

Until this past week, my coding hadn’t expanded much past a repl environment. Last Monday, that changed very quickly because I started an immersive web development program at the Flatiron School.

My first week at Flatiron expanded my scope far beyond coding in my beloved repl.it environment. I was using tools like Terminal and the text editor Sublime Text to do things like interact with directories. Here’s how to use Terminal, explained a bit in an earlier post, and a text editor such as Sublime to create a directory and then output an array of files from it.

A directory is a collection of files (maybe music files or photos, for example) that can be accessed from a Ruby method. Ruby methods Dir.entries and Dir.foreach use the directory file path to return file names:

Let’s pretend I want to create a directory filled with images I could use as profile photos. Some of the files are jpegs and others are pngs (of fun, customizable emojis that I design on madewithcode.com.) I’m going to build a method that will access my ProfileImages directory and output only files with a .png extension:

  1. In Terminal, I go to the place I want to store my directory: cd /Users/rachel/dev
  2. I Make a new directory, which will contain a directory of my images, as well as a Ruby file: mkdir “EmojiFinder”
  3. I change into the EmojiFinder directory: cd EmojiFinder
  4. I make a ProfileImages directory, where I’ll put my images: mkdir “ProfileImages”
  5. I change into the ProfileImages directory and copy in my photos: cp /Users/rachel/Desktop/emoji_mini.png /Users/rachel/dev/ProfileImages
  6. I change back into my EmojiFinder directory: cd ..
  7. I create a ruby file: touch “EmojiFinder.rb”

This method outputs an array of files from ProfileImages directory. From there, I could iterate through the array, outputting only files that contain the .png extension, but I think I’ll solve this a little differently.

This method iterates through the files in the directory, and will only output files with a .png extension. I called the method using the file path to the ProfileImages directory.

Et voila! Running ruby EmojiFinder.rb in Terminal outputs all image files with a .png extension.

Helpful resources:

An HTML Reintroduction

In college, I took one course in HTML, and sweated my way through the absolute basics (I had never even opened a plain text file before starting the class, and had definitely never heard of a “div.”) This week, I reintroduced myself to HTML, and was surprisingly delighted by how much I remembered, considering my general stress and feelings of inadequacy in that years-ago class.

For newcomers to coding, I’d highly recommend using HTML as a starting point, despite everything I just said about fretting my way through Intro to Web Development.

The trial-and-error of learning is different in HTML (hyper text markup language) and CSS (cascading style sheets) than other languages, because instead of getting a scary error message (like I would when improperly writing a function in Ruby), I’m opening my code in an actual webpage and checking out how it looks. The diagnosis is usually, “that looks like crap,” or “hey, that looks pretty good,” or “that looks alright, but I can probably make it better.” And by the way, no matter how bad everything looks, it’s still a very cool feeling, seeing my work displayed on the regal stage of a Firefox browser window.

Another beginner-friendly aspect, is that you can make quite a bit, knowing very little HTML. Here’s a page I made in about five minutes with some light Googling:

Calming pile of rocks

Here’s the code I typed into an HTML file:

HTML example

Starting with header tags (I used <h1> in the first line of code), paragraph tags (see the <p> in the second line), and–if you want to get fancy, some links or images (I inserted the rocks photo with the code in the third line), you can get pretty far in jazzing up a web page. Codecademy is a great place to pick up enough basics to get started.

I remember at the end of my Intro to Web Development course feeling a little boxed-in by HTML and CSS. While it makes a web page look nice, it can’t really do anything. That’s where other languages come in. Now, after pushing myself to learn languages such as Ruby, it’s oddly comforting returning to HTML.

Going to Meetups

When I first started learning to code, I felt way under-qualified to attend programming-related events. I felt like an outsider to the field, and I figured I wouldn’t have much to say or even understand what other people were talking about.

Plus, networking-type functions are intimidating in general. Honestly, any event that requires mingling with strangers on a work night, when I could be running in the park, or getting drinks, or at least going home to change into yoga pants and make mac n cheese, is tough to get motivated to attend.

In September, I pushed myself to go to two Meetup events hosted by the Flatiron School, one called “Women in Tech” that featured a panel of recent graduates working as developers, and another where teams of students presented their projects.

Both times I had some hesitations. (What if I have to make awkward conversation? What if everyone else knows each other? What if it’s not worth my time? Shouldn’t I be at home learning to code instead of standing around talking about it?)

My fretting mostly went to waste because: 1) Awkwardness wasn’t really a problem. All I had to say was “Hi, I’m Rachel. Are you a student?” and that seemed to get the conversation flowing. 2) I underestimated how helpful it would be hearing from people tackling similar challenges to me. 3) Being a part of the developer community (or some very small corner of the community) makes learning to code, in so many ways, just–well– better.

With my brief experience, I’d say going to these types of events is like anything else: the more you do it, the less intimidating it becomes.

 

 

 

Course Report Podcast

 

While reseaching the numerous code schools in New York and deciding which ones I’d like to apply to, I have found Course Report articles and reviews very helpful. Yesterday, I discovered that they also launched a monthly podcast with updates about bootcamps, such as new schools opening and articles published about code school outcomes.

Podcasts are my favorite way to consume news (I listen to them while I make beaded jewelry for my Etsy shop,) so I’ve already blown through the May and June episodes, and plan to listen to the latest episode, July 2016, later today.

I like that the podcast gives a bird’s eye view of the ever-changing code bootcamp landscape. Before finding this podcast, I found it difficult to stay on top of bootcamp news while I focus in on my own learning and preparations. These brief (15-20 minute) episodes summarize relevant information effectively.

20160821_100225
Beading and podcast-listening

 

My Strategies for Learning to Code

When I first started learning to code, I didn’t have much of a strategy. I floated between podcasts, Codecademy, and talking to my friends who worked in the industry. It wasn’t a bad approach, but after taking a few months to learn some basics and look into code bootcamps, I started developing a more organized strategy.

Often, I have felt adrift in my learning process–not sure of what is most important and what I might be missing completely. But lately, I’ve been getting glimpses of the big picture: what code bootcamp will be like, and more distantly, what working as a programmer will be like. That’s given me some confidence in my approach and helped me use my time more productively.

Here are the strategies for learning to code and preparing for bootcamp admissions that have worked best for me:

  • Using a variety of learning tools to find the ones that work best for me (and to switch between when I need a change in scenery.) I’ve used W3Schools, Codecademy, Udacity, Codewars, Coderbyte, Codefights, and Flatiron’s free lessons on Learn.co, as well as podcasts, blogs, and Twitter.
  • Taking notes on anything that may be useful in the future. I created a .txt document so that I can easily add and search for bits of code. I also define terms, summarize concepts, and paste in websites that I may need to refer back to.
  • Being open to both long and short study sessions. Sometimes, I can get wrapped up in involving code challenges for a couple hours without noticing the time pass, but for tasks that I’m dreading, it’s tough to get motivated to push through them. I try to set aside 20-30 minutes to chip away at tricky or otherwise untempting tasks.
  • Identify recurring concepts and mastering them. Recently, I created a 24 problem “quiz” for myself that covered concepts, such as recursion, that I had encountered but not fully mastered. With help from Google, I solved (and then re-solved) the problems until the solutions came easily to me.
  • Trying to finish a study session on a high note; it’s a morale boost that makes it easier to get back to work later. (That said, if I get stuck on a problem that seems unsolvable, I take a break from it. More than once, I have returned to a coding challenge with fresh eyes and quickly noticed an easily fixable mistake.)

CodeFights

In a followup to my post about CodeWars and Coderbyte (two sites that offer coding practice problems), here are my notes on the latest site I’ve been using: CodeFights. CodeFights has several types of challenges, including quizzes, “Tourneys,” and “Head to Head” competitions, but I’ve been using only the the “Code Bots” feature so far.

I like the setup. Each challenge has three parts: one problem fixing broken code, one problem adding to partially written code, and one problem to solve from scratch. At the end, my scores are matched against a “bot” to see who won. It’s nice having a warmup to the hardest problem at the end, and I liked that I’m pushed to stay focused through three problems. The challenges I have finished so far are comfortably within my ability level, and completing each set unlocks a new (presumably harder? If so, the increase in difficulty is marginal) challenge.

I’ve found it user-friendly and glitch-free so far. Occasionally the instructions for problems are a little unclear.