in

Introduction to MongoDB

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

Status: Active!

Monitoring

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

And if we navigate to our monitoring URL we get a nice little 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.

show dbs

Shows database names.

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.

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 are creating a collection called users, and adding information on our first user in the BSON format.

show collections

To list all of the collections that are currently in the database use the command 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()

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

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"})

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"})
whoops

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
the result

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"})
only two users remain

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

NoSQL logo

Introduction to Databases

Introduction to MongooseJS