MongoDB, Mongoose, and the Fun Thing That is Data

Written by dantecualesjr | Published 2018/06/09
Tech Story Tags: programming | coding | lambda-school | mongodb | mongoose

TLDRvia the TL;DR App

I never thought I’d say this, but I had lots of fun handling (read: tinkering, manipulating, playing around with) data and databases this week.

(Isn’t that a super geeky thing to say? I mean, who says that? What kind of human being actually says he likes “managing data”. What a nerd.)

And yet, it’s true. I had lots and lots of fun this week learning about MongoDB and Mongoose.

What Kind of Animal is a Mongoose?

So that’s what we’ve been studying all week at Lambda School. In particular, we covered these topics:

  1. Importing data into the MongoDB database.
  2. Modeling relations between collections.
  3. Embedding documents in schemas.
  4. Linking collections together through refs (references).
  5. Populating data in endpoints.
  6. Querying data.
  7. Creating middlewares.
  8. Custom validations.

Essentially, MongoDB is a database. And Mongoose? Well, it can be defined in several technical ways: a JavaScript framework that you can use to interact with your Mongo database; an Object Data Modelling (ODM) library that helps you model your data; and etc. But what was most helpful for me was to think of it simply as a tool or technology that helps you use MongoDB more effectively and efficiently, in the same way that Express is a tool or technology that helps you utilize Node.js better.

The Problem That MongoDB Solves

Why use MongoDB if you can use Node.js? Well, the problem with Node.js is that as soon as your server goes down, your data goes down with it as well. So there’s no data persistence. With MongoDB, the data persists even if you “kill” the server. So that’s the problem MongoDB solves.

If you’re wondering what it looks like, here it is (running on Windows terminal):

The text “[initandlisten] waiting for connections on port 27017” indicates that your Mongo server is up and running.

Once you have that, you need to open a second terminal to run your “Mongo Shell”. It looks like this:

You can now navigate through your Mongo databases from here. All you need are a few basic commands:

  1. show dbs (to show the list of databases)
  2. use <insert database name here> (to choose a particular database)
  3. show collections (to open a database and list the collections within it)
  4. db.<insert database name here>.find().pretty() (to display the documents or data within each collection)

The First Things You Need to Know When Studying MongoDB

So the most important terms you need to learn when first studying MongoDB are these:

  1. Databases
  2. Collections
  3. Documents

Documents are simply data (e.g., JSON files) that are contained within collections, and collections, in turn, are directories contained within databases.

You can do CRUD operations on your data in the MongoDB database through the Mongo Shell in the same way that you can do CRUD operations on your data through Postman and Compass. That is to say, Mongo Shell, Postman, and Compass are these super cool tools that help you create, retrieve, update, and delete data or documents inside your MongoDB database.

Postman looks like this:

And Compass looks like this:

They’re pretty intimidating to use at first, but are fun to play around with once you get used to them.

They’re certainly very useful to use together when you’re creating your back-end applications and testing your endpoints.

Sprint Challenge

By the way, I’m so happy I was able to get to a Minimally Viable Product during our weekly Sprint Challenge today. I worried that I won’t be able to do it because I initially got stuck in trying to wrap my head around the concepts of models, schemas, refs, and data population. I was overjoyed when I was able to create new databases and collections and store documents into them, and when I successfully tested them in Postman. How can technical things like these be so happiness-inducing?

***

Lambda Ambassador Program

I’ve signed up for Lambda School’s Ambassador Program because I literally cannot stop talking about my school. If you’re interested in getting into their Full Stack Web Development and Software Engineering course, go here: https://mbsy.co/lambdaschool/37941124.


Published by HackerNoon on 2018/06/09