Difficulty: beginner
Estimated Time: 5 minutes

This is lab exercise, you would learn how to download, install and configure the MongoDB Server.

Learning Objectives

Upon completing this exercise students should understand:

  • How to install MongoDB
  • Configuration steps for setting up a simple MongoDB deployment
  • How to run MongoDB
  • How to run the Mongo shell

Congratulations for making it this far!

You've completed your first lab in Getting started with MongoDB!

Don’t stop now! The next scenario will only take about 10 minutes to complete.

Lab: Installing and Configuring MongoDB

Download and Install the binaries

In this step you will download the binaries of MongoDB latest version on an Ubuntu server

Download the MongoDB binaries

Since the current tutorial is running on Ubuntu server let's download the binaries for Ubuntu and uncompress it using below commands.

wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1604-3.6.3.tgz tar xzvf mongodb-linux-x86_64-ubuntu1604-3.6.3.tgz

Setup the environment

Setup the environment using below commands before you start the MongoDB. They will

  • Configure the environment variable PATH
  • Create directory for storing the datafiles

cd mongodb-linux-x86_64-ubuntu1604-3.6.3/bin export PATH=$(pwd):$PATH mkdir -p ~/data/db

Start the MongoDB server

The default settings for datafiles is /data/db. You can specify an alternate path for datafiles using the --dbpath option as show below. Run the below command to start the server with the --dbpath and --logpath overrides

./mongod --dbpath ~/data/db --logpath ~/data/mongod.log --fork

Launch a Mongo Shell

Then type the following to start the Mongo shell.

./mongo

Display available commands.

help

Display available databases.

show dbs

To use a particular database (lets say myappdb) we can type the following.

use myappdb db

To create a collection and insert a document into it, run the below command

db.users.insert({fname: 'Shyam', lname: 'Arjarapu'})

To see all the collections with in the database, run the following command

show collections

To see the document we just created, run the following command

db.users.find()