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()