Getting up and running with MongoDB — Part 2 — NodeJS and MongoDB

I guess nobody cares about earthquakes and cyclones either. LOL.

Enter NodeJS and NPM

I’m not going to talk about NodeJS and NPM here, they’d require a book on their own to describe what NodeJS and NPM are and how they work. I’ll just cut away to the chase slowly even before you know it! So to begin with, install NodeJS on your computer. NPM comes with your NodeJS installation so you won’t need to install that separately. I’m currently on NodeJS version 10.13, make sure you better have the latest version of NodeJS from their website.

# check node version
node -v
npm install -g mongodb

Let’s create a NodeJS Project

Create a directory anywhere on your computer and name it anything you like. Make sure to name it in lowercase and use ‘-’ sign as separator. Let me give you an example —

# correct name
Mongo Start NodeJS
npm init -y
tree command output showing the project dir structure
npm install mongodb --save
# or use the short command
npm i -S mongodb

Inside app.js

So the first thing here is to import the mongodb driver package we installed moments ago via npm. So let’s do that!

npm start
db connect output console
Pardon the trasparent terminal background.

Creating documents

You may have already added documents to your database, in that case you can just carry on with what you have, or can just the json file I’m going to use. Works either way! The concepts will be the same. You can grab the data from from this Gitlab Snippet. Let’s say the file contains some of the games I own on Xbox One. So let me clarify the database structure before you jump on to the train.

// change url value to use the existing one
const url = 'mongodb://localhost:27017/learnMongo';
// replace learnMongo with the database name you already have.
read data output console

Adding a single document

As you can see we have multiple documents here, however before we add them all to the database, let’s add a single document and see how that works. Now here’s a catch, we’ll need to add documents over and over again so why not write a function?

add one doc output console
npm run start or npm start, all the same.
robo 3t output

Adding multiple documents

You just need to call insertMany instead of insert. That’ll do!

robo 3t output

Avoiding Duplicates

The friendliest way to avoid duplicates is to tell your database to consider some properties of the document as unique and it should reject duplicate values based on them (much like unique keys in SQL databases). So what should we mark as unique here? Let’s just say the combo of game “name” and “platform” is going to be unique. Everything else can just go on! And for that, in the mongodb way, we define indexes ( a better way to tell your database you to manage data ). You can read more on database indexing here.

duplicate key error
Mistakenly added RDR, It should be RDR2. My bad!

Reading and Searching : what’s in the database?

Mongodb provides a method called find to fetch data and also to search something. Let’s say we want to fetch every document we’ve added so far in the games collection, it’d be something like this in robo3T -

// or
read all docs output console
db.collection('games').find({ publisher: 'Rockstar' });
search function output console

Now for updating

To update a document, mongodb has multiple functions, as per the docs go

mongodb doc find function list
db.collection('games').findOneAndUpdate( search_filter, update )
update output on console

How to delete then?

Deletion is simpler actually, you just need to call findOneAndDelete. So let’s just delete the XBOX ONE Red Dead Redemption 2 (sorry for the missing 2) entry because in real life, I own the game on PS4. 😝


I think I’ve fried your brain for enough. Time for a song. You can find the code for this article on Gitlab.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shawon Ashraf

Metalhead, Apple Sheep, Horrible Guitar Player, and, I use tabs instead of spaces. P.S. — I hate Vim and Emacs.