MongoDB Compass



  1. Mongodb Compass Tutorial
  2. Mongodb Compass Cost
  3. Mongodb Compass Filter
  4. Mongodb Compass App

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 we use is 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, that 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

At MongoDB, we think that data visualization is a critical part of any data platform. Charts is available free of charge to all Atlas users for evaluation and light to moderate usage converting 1GB of data transfer each month, or about 500,000 typical chart renders. Introduction to MongoDB Compass MongoDB, one of the most widely used NoSQL Database, provides a GUI (Graphical User Interface), MongoDB Compass. MongoDB Compass allows you to interact actively and understand the data stored in MongoDB Database, without any prerequisite to have any knowledge of Coding or Shell Queries. Its products include MongoDB Enterprise Advanced, MongoDB Enterprise for OEM, MongoDB Professional, MongoDB Stitch, MongoDB Atlas, Development Support, Ops Manager, Cloud Manager, Compass. This feature alone in Compass makes it worth the download, but there is much more! Creating and dropping databases and collections is possible in Compass and, as I pointed out in a previous post on indexing MongoDB collections, indexes can have a huge impact on the performance of queries. Compass provides an intuitive interface for index creation.

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 dependancy to my Dynamic Web project in Eclipse IDE and then the JDBC connector…. good times 🙂

Compass

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.

MongoDB Compass

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.

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,

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.findOne…findOne is synonymous to the where sql where
  • db.sample.find…find 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

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.

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.

Instead what you need to do is add a filter

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.

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

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

Mongodb compass download

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

Mongodb Compass Tutorial

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.

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/

Mongodb Compass Cost

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

Categories:

Mongodb Compass Filter

JavascriptNodeJS

0 Comments

Mongodb

Mongodb Compass App

Leave a Reply