Day 27 — Mongo Is Not Your Friend
In our first foray into using databases we dived into Mongo. For those in the know about databases will know that Mongo, by itself, is not the greatest idea. Mongo lacks structure but is great for cramming a bunch of data, hopefully already grouped together, into a collective database.
Why would you want something like that? Perhaps you have zero desire to do a relational queries. I honestly don’t know. Tomorrow we discuss Mongoose to go over schemas. But for today Mongo.

I want to be clear on something. I have a lot of experience with relational databases. They make sense to me. This willy nilly thing is…well whatever.
There are basic commands to Mongo that to me make sense, whether in shell (terminal) or within Node. It’s pretty simply. The first hurdle that we had to overcome, actually became the last for us, was the fact that at no time for the first couple hours were we told not to re-run our server with the line: return db.collection(“users”).insertMany(data.users).
Just in case you are reading this and have no idea what it means, it literally says to add all of the users data into the database. So if you stop the server and run it again, hey look it appended more data. Didn’t catch this until I had appended probably 40 times. That was easy. What I truly struggled with was this:
db.collection(“users”).findOne({job: null})
Let’s break down what this actually means, what I wanted, and what happened.
db literally means database, collection(“users”) is the collection/group that is being worked on, findOne is the query on the collection, ({job: null}) is the filter.
({job: null}) is actually in reference to the data that I wanted. Ultimately I wanted to get a list of those users that have null for job. For whatever reason this works only if I did .findOne({job: null}). If I chose to do .find({job: null}) it failed. I failed a lot in this. I think I went four hours over this one line of code.
I knew once I got this one line right I get then populate the page with the data. The resolution?
First put it all in a function. Return the line with all, not just the one, and apply it to an array (duh!), then render the page.
Fours on one line and then 10 minutes fixed it.
Struggles: Honestly my struggles were less on the daily assignment but it’s starting to be issues with this weeks assignment. I have some of the basic structure down but it’s littered with errors. I’ll need to put in a lot more time into it.
Understood: The command lines for getting through Mongo are simply enough.
Looking forward to: Relational databases. I’m ready for you SQL.