Getting started with MongoDB

This post describes the basics in installing MongoDB in your Debian system, creating a database, a collection with some documents, dropping collections and databases.

If you wish to install mongoDB on your Debian system you should (as root) install the mongodb metapackage (it will install all necessary packages to your system).

apt-get install mongodb

Now the mongoDB deamon is started by default upon installation and it uses some default directories to store the databases and the log files. An easy way to see where these directories reside on your system is to see the parameters that the deamon has started with. To do so issue the following command.

ps -ae | grep mongod

If you are a Debian user (like me) you will probably receive an output like

/usr/bin/mongod --dbpath /var/lib/mongodb --logpath /var/log/mongodb/mongodb.log --config /etc/mongodb.conf run

which means that your server uses the
/var/lib/mongodb directory to store databases
/val/log/mongodb/mongodb.log file to store log entries
/etc/mongodb.conf file to store settings

If you wish to start the mongo shell from command line type

mongo

This will start the mongo shell and will connect you to the default database which is test.

If you wish to create a new database you should start the mongo shell with one parameter: the name of the database you wish to create

mongo lala

will create a new database named lala. Now this database does not actually yet exist somewhere in your system. If it did exist you should see some files created under the /var/lib/mongodb/ directory mentioned earlier. So what happened to our database? The database files will be created as soon as you create a new collection that belongs to this database. So let’s create a new collection named student.There are two ways to do so.

Fisrt:

var a = {name: 'John', language: 'English'}
db.student.save(a)

Second:

db.student.save({name: 'John', language: 'English'})

Both ways will create a new collection named student (student can for now be considered analogous to a table named student in the relational database model) and would save a new document in the collection (a document can be considered analogous to a row in the relational database model).

As soon as you issue the above and thus create a new collection in your database with one document inside it you will see the files for the database created in your /var/lib/mongodb/ directory. These files are named lala.0, lala.1 and lala.ns.

To view all documents in your student collection (= something like a SELECT * FROM student in relational databases) do

db.student.find()

To delete the collection student entirely ( = something DROP TABLE student in relational databases) do

db.student.drop()

To delete the database lala entirely (= something like DROP DATABASE lala in relational databases) do

db.dropDatabase()

When you issue the dropDatabase command the files from the /var/lib/mongodb/ directory will be erased.

NOTE:
There is a command you can use from the shell to identify the name of the database that you are currently working in (or to express it differently the name of the database that the collections you create will be saved in). So if you try to issue

db.getName()

command after you delete the database then you are going to get lala as a result which means that if you try now to create a new collection the collection will be stored in a database called lala which is a different database than the one you had earlier which does not exist anymore.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s