« Playing Around with Habari Create a Simple App with Django and MongoDB: Part 1 »

February 18, 2010

By Reza Muhammad

4 Comments, No TrackBacks

Basic Commands to Get You Started with MongoDB

MongoDB is a schema-free, document-oriented and non-relational database engine. For more information about MongoDB, their website, a Wikipedia Entry or these screencasts will probably explain it better than I do.

I wanted to try MongoDB out of curiousity. While there are other options to document-oriented database engine such as CouchDB, or Tokyo Tyrant, I decided to go with MongoDB because I find their documentation is easy to follow.

After fiddling with MongoDB for a few hours, I thought I'd write down a few basic commands to get you started with MongoDB. Coming from a RDBMS background like MySQL and PostgreSQL, I found some of these commands are quite useful for me to get me going with MongoDB. Hopefully, it can help you too if you are thinking to give MongoDB a shot.

As a quick note, a few symbols are repetitively used, and these are the common conventions (if you will):

  1. $ identifies that the command is executed from the command prompt
  2. > identifies that the command is executed from MongoDB shell prompt

Installing MongoDB

Download from MongoDB Website. Once downloaded, extract the file and you will be presented with a directories containing MongoDB executables.

Assuming the folder created is called mongodb and it is located in /home/rezmuh/mongodb. You should have the following commands and folders:

$ ls
GNU-AGPL-3.0        bin         mongodb.log
README          include
THIRD-PARTY-NOTICES lib

Run MongoDB as a daemon

By default, MongoDB is running on port 27017, and database files are located in /data/db/ directory. So, make sure there is no application running on that port, and /data/db exists.

$ pwd
/home/rezmuh/mongodb
$ mkdir -p /data/db/
$ bin/mongod --fork --logpath ./mongodb.log --logappend

Create a new user

$ bin/mongo
> use admin
> db.addUser('dummy, 'dummy')
{ "user" : "dummy", "pwd" : "734b8c8ddfce845642c258769bb3e936" }

If you see a similar message to the above, it means that a user 'dummy' with password 'dummy' is successfully created. Now, we want to make sure that MongoDB is running with autentication.

In order to do that, we need to restart MongoDB daemon to use authentication:

$ killall mongod
$ bin/mongod --fork --logpath ./mongodb.log --logappend --auth
forked process: [...]
all output going to: ./mongodb.log

Connecting to a Database with authentication

$ bin/mongo -u [username] -p [password] [db name]

As a reminder, MongoDB seems to handle users within a database. So from the previous commands, we created a user 'dummy' for 'admin' database.

So at this point, we can only run:

$ bin/mongo -u dummy -p dummy admin

This will connect admin database as a user 'dummy'.

Create a new Database

> use new_database

In MongoDB, use new_database will switch our access to the 'new_database' database. If the database does not exist, it will automatically create it for you.

To create a new user to new_database, run:

> db.addUser('dummy', 'differentpassword')

To access your new_database with a specified user, run:

$ bin/mongo -u dummy -p differentpassword new_database
MongoDB shell version: 1.2.2
url: new_database
connecting to: new_database
type "help" for help

The above commands show that a user 'dummy' is created to access 'new_database' but it has a different password than the one who can access database 'admin'

Deleting a database

> use [db name]
> db.dropDatabase()

Working with Collections (Tables)

> use new_database
> db.profile.save({name: 'Reza Muhammad', blog: 'http://rezmuh.sixceedinc.com'})
> db.profile.find()
{ "_id" : ObjectId("4b7d2bdfe003af231920152a"), "name" : "Reza Muhammad", "blog" : "http://rezmuh.sixceedinc.com" }
> show collections
profile
system.indexes
system.users

The above commands will insert 'Reza Muhammad' to a field name, and 'http://rezmuh.sixceedinc.com' to a field blog in a profile table. If the table does not exist, it will create it automatically. The show collections commands will list all the collections exist in the database. In this case, profile collection exists because we already created one (through db.profile.save())

Since MongoDB is a schema-free database engine, you can also create a new record in profile collection that has different fields than previous mentioned.

Some Useful Commands

  1. Use bin/mongo to to a MongoDB server without authentication
  2. User bin/mongo -u [username] -p [password] [db name] to connect to a [db name] database with a specified username and password
  3. show dbs lists all the available databases
  4. use [db name] will let you access the database if it exists. Otherwise, it will create a new database
  5. show collections will give you a list of collections (tables) in the database
  6. To get a list of data in a specific collection, use db.[collection name].find()
  7. To get a list of all available methods within a collection, run db.[collection name].help()
  8. db.getName() will inform you the database name you're currently accessing
  9. db.dropDatabase() will drop the current database you're accessing
  10. db.addUser('[username]', '[password]') will create a new user for to access the current database
  11. db.help() lists all the available methods you can use within a database

So there you go, those were the commands to get me started with MongoDB.

TrackBacks

TrackBack URL: http://rezmuh.sixceedinc.com/mt/mt-tb.cgi/15

4 Comments

Great post. Makes me want to try using MongoDB? Is there an official (maintained) package for Ubuntu?

Thanks Reza, great summary.
Btw, have you tried to make a simple address database via web using mongodb using php, python or javascript? I wonder how to do it..

Leave a comment

Scroll to Top