Beginning MongoDB

Coming at MongoDB from a RDMS perspective (and specifically MySQL), things feel a little strange. Just sharing a few paragraphs on how I got started.

Installation

Installing MongoDB was easy following their instructions and using the 10gen repo for CentOS. After installation the mongo client can be used to access the server without any authentication i.e.

mongo

After connecting the following commands can be used to have a look around:

show dbs
use dbname
show collections
show users
show roles

Creating a Root User

I then created a root user with:

use admin
db.createUser(
    {
      user: "root",
      pwd: "mysuperstrongpw",
      roles: [ "root" ]
    }
)

Creating a Database and User Account

After creating the root user, it is no longer possible to use the mongo command to connect to the server without authentication, so connect as root with the following:

mongo 127.0.0.1:27017/admin -u root -p mysuperstrongpw

Then create a database and user:

> use mynewdb
> db.addUser({user:"jonny", pwd:"opensesame", roles:[ "readWrite","dbAdmin" ]})

The database is not actually created until something is written to it (in this case a user).

Connect as the New User

I should now be able to access the MongoDB database from another host as follows:

mongo serveraddress:27017/mynewdb -u jonny -p opensesame
> use mynewdb
switched to db mynewdb
> show collections
mycollection
system.indexes
> db.mycollection.find()

Export Data from Existing Database

The following command exported data in JSON format from an existing Mongo database:

mongoexport --host serveraddress -u jonny -p opensesame --db mynewdb  --collection mycollection --query '{"somefield":"somevalue"}'  --out data.json

 

Import Data

I could then import a JSON data file using the following:

mongoimport  --host serveraddress -u jonny -p opensesame --db mynewdb --collection mycollection --file ./data.json

 

Graphical Interfaces

If you want a GUI interface I have tried RoboMongo and MongoChef

MongoChef

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>