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:
- Importing data to the MongoDB database.
- Modeling relations between collections.
- Embedding documents in schemas.
- Linking collections together through refs (references).
- Populating data in endpoints.
- Querying data.
- Creating middlewares.
- Custom validations.
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:
- show dbs (to show the list of databases)
- use <insert database name here> (to choose a particular database)
- show collections (to open a database and list the collections within it)
- 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:
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 when 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.
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.