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):
$identifies that the command is executed from the command prompt>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
- Use
bin/mongoto to a MongoDB server without authentication - User
bin/mongo -u [username] -p [password] [db name]to connect to a [db name] database with a specified username and password show dbslists all the available databasesuse [db name]will let you access the database if it exists. Otherwise, it will create a new databaseshow collectionswill give you a list of collections (tables) in the database- To get a list of data in a specific collection, use
db.[collection name].find() - To get a list of all available methods within a collection, run
db.[collection name].help() db.getName()will inform you the database name you're currently accessingdb.dropDatabase()will drop the current database you're accessingdb.addUser('[username]', '[password]')will create a new user for to access the current databasedb.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
Great post. Makes me want to try using MongoDB? Is there an official (maintained) package for Ubuntu?
Hey, how are you Ak?
I don't think there is an official Ubuntu packages yet. But when you download the source, they already have the executables ready under bin folder. It was pretty much the same way I installed my MongoDB.
Then, from bin directory, you will find mongo, mongod and other executables. Go ahead, try it out. It's been quite an exciting experiment for me :)
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..
Hi,
I haven't tried it with PHP or Javascript yet, but the idea is to have a driver for your language and that driver usually gives you the API on how to use it.
For PHP, you can look at: http://www.mongodb.org/display/DOCS/PHP+Language+Center. They have quite a long list on how to use Mongo and PHP.
As for Python, I only tried briefly witb pyMongo (the driver) and MongoEngine (ORM equivalent to accessing Mongo). MongoEngine also provides authentication and session "helpers" for Django, if you're into it.
Actually, in the upcoming post, I'm going to talk about using Django with MongoDB.
4 Comments