Day 27 (week6) — MongoDB

Eric Abell
Aug 23, 2017 · 4 min read

Morning announcements consisted of some reminders about Growth vs. Fixed Mindset. I think this was in response to several people getting frustrated yesterday, but I’m not sure.

We then took a look at the Caesar Cipher solutions from yesterday’s project. I was really surprised that no one used ASCII character codes except for me. I’ll admit that it’s not really an obvious solution to the problem, but the idea of converting the letter into a number and then shifting by a certain amount just kind of naturally leads you to using ASCII codes.

Things moved into using MongoDB. Running mongo from the command line and issuing commands from the mongo prompt and importing/exporting data using mongoimport and mongoexport. There was some example data from a JSON file from last night’s reading — which we imported and played around with a little bit. The data was listings for restaurants in New York.

We used all the different query methods like find and findOne to get data out of the database. We looked at filtering results and some of the query selectors that Mongo provides like $in, $eq, and $exists. We also sorted the data using a query to Mongo.

We discussed what an “upsert” operation looks like — this is an update if there is a matching document already in the collection and an insert if no matching document is found. It’s kind of like telling Mongo: “hey, if you’ve got a document that matches my filter, update that document. otherwise, just go ahead and insert a new document with the data I provided”

Using mongodb from NodeJS was interesting. Basically, you’re just running all the same queries that you ran at the mongo shell prompt but through your Node program instead. Some interesting things can happen here because of the asynchronous operation of Node. For example, if you query Mongo, the result isn’t immediately available and so Node will not block program operation and continue on with the rest of your program. If you’re not prepared for that, it can be quite undelightful. I’m not even sure undelightful is a word, but it seems to fit my sentiments about when these things happen in your program. :-)

The daily project was released before lunch and I finished it in 40 minutes with 6 commits. I was pretty happy about that. After lunch, we had a wonderful lightning talk about personal branding from Sara Harless. She had a worksheet handout that we completed with some questions to help guide us in developing our personal brand.

I spent the rest of the afternoon completing all the extra challenges from the daily project. This involved our user directory of robots from a few days ago. In the normal version of the project, we just had to move all the data from a file into Mongo. Then, we needed to update our server code to query the database instead of loading from the file. A navigation bar was added to the top that gave the user three options for the listing: (1) all robots; (2) employed robots; and (3) unemployed robots. This ended up looking something like this:

After that, we had to implement clicking on a country name to query for only those robots from that country. This was super easy because all the countries were single words with no spaces or strange characters. The second part involved making all the skills clickable links that found all the robots with those skills.

This was a bit of a challenge, since the links for each skill couldn’t have any spaces in them. Since the name of the skill had a space, I had to come up with a solution. I thought mustache might have something, but it turned out not to have anything helpful. In the end, I just modified the skills array of the data right before I sent it back to the client. I replaced each item in the list with a new JS object that had the name of the skill and the URIencoded version. That bit of code looked like this. Notice the check for unique skills — this is how I figured out that there weren’t any duplicate skills in the data.

let encodeSkills = function(data) {
let uniqueSkills = [];
data.users = data.users.map((user)=> {
let listOfSkills = user.skills; // array of skills
let newSkillsList = [];
listOfSkills.forEach( (skill) => {
if( uniqueSkills.indexOf(skill) >= 0 ) {
console.log('Found duplicate skill: ' + skill);
}
newSkillsList.push({'text': skill, 'uri': encodeURIComponent(skill)});
})
user.skills = newSkillsList;
return user;
})
return data;
}

I finished this as well. But…..

Unfortunately, the folks that put the data together didn’t include any robots with duplicate skills. I changed the data file to make it so that some robots had duplicate skills and then ran my app. Everything worked. I submitted my code, which you can find here.

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade