fbpx

I have spent a lot of time working with MongoDB and NodeJS lately. The most recent project at my work is all about building APIs. Mostly, REST APIs and the database backend for which we use MongoDB. For the purpose of the project, I am mostly working with a MongoDB instance stored somewhere on the cloud. Hence, I have a connection url to connect to the MongoDB instance and I can connect to it without any dramas. This was one of the first times I worked so heavily with MongoDB database. Prior to this, I have worked with various relational databases and a NoSQL database that wasn’t MongoDB. On starting with MongoDB, I found that setting up and connecting to a local MongoDB database wasn’t very straightforward. In this post, I will talk about how to setup a local instance of MongoDB, run it, insert data into it via the Mongo shell, view it using a GUI like MongoDB Compass and connect it to a NodeJS backend.

Background

I am an old timer, I have been around since the days when Java or J2EE would take a couple of days to just setup. I am of course talking about a time before Spring initializr made life easy. A lot of my experience has been around relational databases, be it MySQL, SQL Server, Oracle or PostgreSQL. Ahh… I fondly remember the days of finding that mysql connector jar file, adding it as a dependency to my Dynamic Web project in Eclipse IDE and then the JDBC connector…. good times 🙂

Anyway, fast forward 10 years and I now find myself working on a “modern backend” which includes NodeJS and MongoDB. I cannot say either of these technologies are entirely new to me, I have built a backend with Node before and I have worked with a NoSQL database before. I started working with this tech at my workplace on a new project. Now, my workplace can be a little fast paced, in that every request is a priority, it has to be done ASAP. The focus is heavily on building fast and shipping things. As a result, I have written heaps (lots) of code and solved number of problems over the last 4 months. All those solutions fixed the issues assigned to me but I felt I should have my own MongoDB and NodeJS playground to understand the basics of it. It’s difficult for me to find the time to do that at work, so I am doing this after hours. Now, I hadn’t worked with this combination (NodeJS + MongoDB) before, I came across a few hurdles when setting up a local MongoDB instance. I my mind I knew how to setup a local MongoDB database would be quite simple yet for some reason it simply wasn’t as straightforward as I thought.


p.s. I said the company I work for can be too fast paced at times but that’s also the reason why the business has grown so much. Seven years ago, there were less than 5 people in the company and it now employs 400+ people.

Working with new tech

When I work with new tech or solving any problem really, my first goal is to decompose it into sub-problems and solve the most basic problem. In this situation my goal was that I am building a solution with NodeJS and MongoDB and the first thing I could think of was to have MongoDB database running on localhost.

Setup MongoDB on a Macbook

I use a Macbook at work and a Ubuntu at home, so both unix based operating systems. Hence my installation skills rely on using the command line. Remember, I am doing this just to practice working with MongoDB, so the free community version of MongoDB should work just fine. The installation instructions I provide rely on using Homebrew. Hence, in case you have not already done so, install HomeBrew on your mac by following the instructions here. Next up,

How to setup local MongoDB database (Mac)

Then run the following commands on the command line.

  1. sudo brew install mongodb-community
  2. sudo mkdir -p /data/db
  3. sudo chown -R id -un /data/db

What’s happening in Step 2 and 3? we are just making sure the user running the MongoDB service has the right directory permissions. You can read more about chmod here.

Awesome, now we have everything installed and know how to setup local MongoDB database. The next step is to start the MongoDB service.

  • brew services run mongodb-community

MongoDB should be have started now, in case you need to see the all the services that are running, enter this on the command line

  • brew services list

…and now you should be able to see that the MongoDB service is running.

Like the blog? Subscribe for updates

Let’s insert data into our local MongoDB database

Now, inserting data into a MongoDB database is somewhat simpler, especially if you have been coding in Javascript. How do we insert a record into it. To do so, run the following commands,

mongo
use db test //use test database
db.sample.insertOne({name: "Bhuman", title: "Fullstack Developer"})
db.sample.findOne({name: "Bhuman"})
db.sample.find()

Let’s examine each of the above commands

  • mongo to be in the mongo command line shell
  • use db test to use the test database
  • db.sample.insertOne… to insert a record in the sample collection. A collection in a NoSQL database is a table in a relational database.
  • db.sample.findOnefindOne is synonymous to the where sql where
  • db.sample.findfind is now the select * equivalent i.e. returns all records

Command line is great but….

Most people won’t always want to live on the command line, they would need some sort of GUI interface. In this case it makes sense to have one, as it would save us memorising commands such as

use db
show collections
show users
show dbs

so and and so forth. You can view list of Mongo shell commands here. Thanks for the reference to the commands but where’s the GUI?

MongoDB Compass

The community edition of MongoDB compass is one of the free GUI tool for MongoDB. It’s easy to use and intuitive. Have a look at the below screenshot as an example Another GUI tool is Robo 3T, it’s got a 30 day trial version but otherwise, it needs a paid license, hence I prefer MongoDB Compass here.

Intuitive MongoDB Compass GUI after setup local MongoDB database

From here, you can add, query and browse various collections (tables). Querying these collections will take a little bit of getting used to. Especially if you are someone who’s only worked with relational databases.

select name,title from persons where name='Bhuman' 

Instead what you need to do is add a filter

{name: 'Bhuman'} 

So it only returns the records with the name ‘Bhuman’. To know where to type that query condition in MongoDB Compass, look at the screenshot below.

Querying MongoDB Compass

This is great but what if we need to display all the data stored in our MongoDB database on the web sometime? Why yes, then let’s build a backend for it.

NodeJS and MongoDB

This is what I have been working with a lot lately and I reckon we can illustrate the concept with the least amount of code with NodeJS.

Setup a NodeJS project…

On the command line

mkdir nodeMongoTest && cd nodeMongoTest //create direct
npm init -y //initialise a NodeJS project
npm install --save mongodb //add a mongoDB driver
vim index.js //create a node file 

After the last line, you will start editing the file from command line.

Alternatively, you can just follow this article here to know how to setup NodeJS to work with MongoDB.

Connect to MongoDB database

Let’s write simple code that just connects to our test database

const MongoClient = require('mongodb').MongoClient;

const url = 'mongodb://127.0.0.1:27017'; //localhost

const dbName = 'test';

let db;

MongoClient.connect(url, {useNewUrlparser:true}, (err,client) => {
  if(err) return console.error(err);
  
  db = client.db(dbName);
  console.log(`Connected via ${url}`);
  console.log(`Database name ${dbName}`);

  let bhuman = db.collection("sample");
  console.log(bhuman);
});

I spent way way too much time on the command line when I was doing research in robotics, hence, doing this with vim is second nature. However, I reckon using something lightweight like sublime text or gedit would work just fine.

Summary

As mentioned earlier one of the hurdles for someone like me who’s coming to MongoDB from the relational database world was to get a local instance going. That was what I had hoped to address in this post i.e.

  1. How to setup local MongoDB database
  2. insert data into it via the command line mongo shell
  3. examine the local database via Mongo GUI e.g. MongoDB Compass
  4. finally, connect to the running instance via NodeJS

I hope this equips some coming from the relational database with the knowledge of setting up a local MongoDB instance. In the next post, we will focus on how to CRUD documents in a collection in a MongoDB database with NodeJS using both the MongoDB npm package as well as Mongoose.

Like the blog? Subscribe for updates

As usual, if you find any of my posts useful support me by  buying or even trying one of my apps on the App Store. 

https://mydaytodo.com/apps/

Also, if you can leave a review on the App Store or Google Play Store, that would help too.


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *