Linux: MongoDB Basics

By Xah Lee. Date:

This page is a basic tutorial on MongoDB. This page assumes you are using Ubuntu Linux.

this tutorial is INCOMPLETE, WORK IN PROGRESS. It is my personal learning notes. Most of it based on the official documentation at


Install from Ubuntu Bundled Package

if you want to install from what's bundled with Ubuntu, do this:

(this means it won't be latest version of mongodb)

# search for mongodb
apt-cache search mongodb

# describe the package
apt-cache show mongodb

# check if it's installed
dpkg -l '*mongo*'

# install mongodb
sudo apt-get install mongodb

Install latest MongoDB from 10gen

if you want to install the latest, follow official site at

here's the basic steps. (note: 10gen is the company that created mongodb)

# import 10gen's public key
sudo apt-key adv --keyserver --recv 7F0CEB10
# add 10gen's repository to apt
echo 'deb dist 10gen' | sudo tee /etc/apt/sources.list.d/10gen.list
# refresh
sudo apt-get update
# install latest mongodb
sudo apt-get install mongodb-10gen
# install a specific version
apt-get install mongodb-10gen=2.2.3
# pin a package, prevent 「apt-get update」 from updating your mongodb
echo "mongodb-10gen hold" | dpkg --set-selections

MongoDB File Locations

Config file at /etc/mongodb.conf

Control script at /etc/init.d/mongodb

Data files at /var/lib/mongodb

Log files at /var/log/mongodb

Commands and Programs

the following commands are installed. They are at /usr/bin/


# start mongodb
sudo service mongodb start
# stop mongodb
sudo service mongodb stop
# restart mongodb
sudo service mongodb restart
# check running process
◆ ps -ef | grep mongo
mongodb   1145     1  0 21:15 ?        00:01:15 /usr/bin/mongod --config /etc/mongodb.conf

You can verify running status by looking at log /var/log/mongodb/mongodb.log.

Using MongoDB

Type man mongo to read its doc.

Start the mongo shell by mongo, then type help. Sample output:

◆ xah@xah-p6813w◆ 2013-06-05 00:08 ◆ ~
◆ mongo
MongoDB shell version: 2.0.4
connecting to: test
> help                    help on db methods             help on collection methods                    help on replica set methods
        help admin                   administrative help
        help connect                 connecting to a db help
        help keys                    key shortcuts
        help misc                    misc things to know
        help mr                      mapreduce

        show dbs                     show database names
        show collections             show collections in current database
        show users                   show users in current database
        show profile                 show most recent system.profile entries with time >= 1ms
        show logs                    show the accessible logger names
        show log [name]              prints out the last segment of log in memory, 'global' is default
        use <db_name>                set current database                list objects in collection foo { a : 1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to further iterate
        DBQuery.shellBatchSize = x   set default number of items to display on shell
        exit                         quit the mongo shell

The mongo shell language is JavaScript.

If you have a question, put $5 at patreon and message me.