Mongodb Brew



  • If having the mongodb cask referenced in brew install mongodb is an issue, I’d prefer if a note about it were added in install, just like it was in search. Kbd added a commit to kbd/setup that referenced this pull request Sep 2, 2019.
  • For example, to install MongoDB with Homebrew, you need to tap into mongodb/brew formula: $ brew tap mongodb/brew Then you can install MongoDB like this: $ brew install mongodb-community To remove an already tapped repository, use the untap command: $ brew untap mongodb/brew To view all tapped repositories, you use the following: $ brew tap.
  • On macOS, the best way to install the MongoDB Shell is via Homebrew: $ brew tap mongodb/brew $ brew install mongosh For detailed installation instructions, check the documentation. User Experience. Good user experience is key in any type of software, including the tools we use in the terminal.

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

Brew services start mongodb-community@4.4 it will create a new /tmp/mongodb-27017.sock which is owned by user.

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 🙂

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.

Mongodb Brew

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

Brew Tap Mongodb Brew

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

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.

Mongodb

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.

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.

Categories: JavascriptNodeJS

0 Comments

Leave a Reply

Overview¶

Mongodb Brewing

Mongodb

Mongodb Brewing Company

The MongoDB Database Tools are a suite of command-line utilities for working withMongoDB. Use this guide to install the Database Tools on the macOSplatform.

Versioning¶

Starting with MongoDB 4.4, the MongoDB Database Tools are now released separatelyfrom the MongoDB Server and use their own versioning, with an initialversion of 100.0.0. Previously, these tools were releasedalongside the MongoDB Server and used matching versioning.

For documentation on the MongoDB 4.2 or earlier versions of thesetools, reference the MongoDB Server Documentation for that version of the tool:

Compatibility¶

MongoDB Server Compatibility¶

Mongodb Mac

MongoDB Database Tools version 100.3.1 supports the following versionsof the MongoDB server:

  • MongoDB 4.4
  • MongoDB 4.2
  • MongoDB 4.0
  • MongoDB 3.6
Brew

While the tools may work on earlier versions of MongoDB server, anysuch compatibility is not guaranteed.

Platform Support¶

The MongoDB Database Tools version 100.3.1 are supported on macOS 10.12 orlater.

Installation¶

The MongoDB Database Tools can be installed with the third party Homebrew packagemanager, or downloaded as a .tgz archive. Select the tab belowdepending on your desired installation method:

Starting with MongoDB 4.4.1,installing the MongoDB Server via Homebrew also installs the Database Tools. Thefollowing command will determine if the Database Tools are alreadyinstalled on your system:

Install Homebrew.¶

macOS does not include the Homebrew brew package by default.Install brew using the officialHomebrew installation instructions.

Tap the MongoDB formula.¶

In your macOS Terminal, run the following command to download theofficial MongoDB Homebrew formulae for MongoDB and theDatabase Tools:

Brew Mongodb Tools

Brew

Install the MongoDB Database Tools.¶

In your macOS Terminal, run the following command to install theMongoDB Database Tools:

Once installed, you can later update the Database Tools with:

Run the installed Tools.¶

Mongodb Brew

Once installed, you can run any of the Database Tools directlyfrom your macOS Terminal. Consult the reference page for thespecific tool you wish to use for its full syntax and usage.

© MongoDB, Inc 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.

Mongodb Client Mac

Installing the Database Tools on LinuxInstalling the Database Tools on Windows