Skip to main content

MongoDB Introduction

Intro

MongoDB is a Non-Relational Database that stores data in objects and arrays. MongoDB is the most popular database to use with Node.js, Express.js, and modern web applications in general.

Installation

Installation of MongoDB will differ depending on your programming language and server environment. So far we have been using a Node.js server on AWS Cloud9, so we will continue with that platform here. Instructions for other stacks are available in the official MongoDB Manual.

The official instruction to install MongoDB on AWS are available here: Install MongoDB on Amazon

Although I found that the set of instructions that works for me on Cloud9 is actually the Ubuntu instructions because I am running Ubuntu on my VS: Install MongoDB Community Edition on Ubuntu.

mongo status active

Monitoring

Right after installation we are given the ability to activate free monitoring… that looks pretty cool, so lets do that:

mongo monitoring instructions

And if we navigate to our monitoring URL we get a nice little dashboard.

mongo monitoring dashboard

What is happening here

My intuition would be that we would need to set up a whole new server which is designed just to house the database and create some kind of secure connection to that server, but in fact we are simply installing the database on our current server. Almost like a partition on a hard-drive.

Mongo Shell Basics

mongod

Starts the Mongo Demon, the Mongo process, which will run in the background continuously. AKA starts the database.

mongo

Opens the Mongo console, where we can type the rest of our commands.

help

Lists the basic features of Mongo.

mongo help command list

show dbs

Shows database names.

list of mongo dbs

use (databaseName)

use does two things. If a database already exists it will switch to that database. If it does not yet exist, it will create a database with that name, and then switch to it.

switched to db demo1

insert

insert is used to add data to the database. Even though this is a non-relational database, data must be grouped into collections, even though collections do not have to be identical. Here we are creating a collection called users, and adding information on our first user in the BSON format.

inserting user

show collections

To list all of the collections that are currently in the database use the command show collections.

show collections

find

The find command allows us to look up data in a collection. Here we have requested ALL of the data in the users collection. We can see that this object in our collection has been assigned a unique id.

db.users.find()

find users

lets add some more users to our database to make this a bit more comprehensive

showing four users

now lets find a specific piece of information instead of listing the whole collection. First we will find all users with status “gold”, and next we will look for the user named Karen.

db.users.find({status:"gold"})

db.users.find({name:"Karen"})

filtering for gold and karen

Note that when we make the request, it is made in the BSON format, not a simple string.

update

The update method is used to expand or modify an existing object. It follows this basic format.

db.collection.update(**{object to modify}**, **{modifications}**)

db.users.update({name:"Gary"}, {status:"platinum"})

overwriting previous data

In the above example we can see that we actually overwrote the whole record instead of updating the status of the individual.

$set

In order to preserve our previous object data and only update specific items we have to use the $set command inside our modifications object. Let’s update the name and status for user Karen, without changing her username.

> db.users.update({name:"Karen"},{$set: {name:"Jasmine", status:"aluminum"}})

requesting the change

karen has been updated without overwriting whole record

remove

Of course we need to be able to delete items from the database as well. We currently have two users with a status of platinum. Let’s delete them both.

db.users.remove({status:"platinum"})

platinum users removed

delete collection

To delete an entire collection in one go, navigate to the collection and then use:

db.collectionName.drop()

More on this in the Mongo Docs.

Troubleshooting

Restarting Mongod

If you are having trouble getting your MongoDB Mongod to restart try the following command:

sudo service mongod start

To learn more about this there is a Stack Exchange article: Mongod Connect Failed

Check Server Status

To check is the database is running input the following command in the unix console (as opposed to the mongo console).

service mongod status

Installation Troubleshooting

One of my installation attempts on an Amazon EC2 instance was failing to run MongoDB even after removing and re-installation several times and scouring the web for solutions. After failing for hours I put it down for the night, came back the next day and it started up right away. So try restarting your server! Of course. Have you tried turning it off and turning it on again!

Comments

Recent Work

Free desktop AI Chat client, designed for developers and businesses. Unlocks advanced model settings only available in the API. Includes quality of life features like custom syntax highlighting.

Learn More

BidBear

bidbear.io

Bidbear is a report automation tool. It downloads Amazon Seller and Advertising reports, daily, to a private database. It then merges and formats the data into beautiful, on demand, exportable performance reports.

Learn More