Basic commands mongodb show tables, views, databases, create users and more.

Overview

Everytime I have to work with a new database I struggle, specially if the database is bit different from rest of its siblings. Mongodb is a new kid in town and doesn’t understand SQL one bit because its a NOSQL database. Now that’s bad news for me, I am used to working with MySQL, postgersql and Oracle and suddenly NOSQL. If you are stuck in a same scenario this post will let you start working with mongodb.

Basic commands mongodb show tables, views, databases, create users and more

To login to mongodb shell

# mongo

Mongodb does not come with any type of authentication out of the box. To enable authentication you need to perform these additional steps.

Step 1: Open the configuration file and

Open the mongod.conf file in an editor of choice


# vi /etc/mongod.conf

Look for auth directive, set it to true and uncomment it


auth = true

Restart mongodb service for the settings to take effect.


# /etc/init.d/mongod restart

Step 2: When setting up authentication for the first time you need to have at least one user in the admin database.

Login to mongo db and add user to admin database.


# mongo
>use admin
>db.addUser('admin', 'password')
>db.auth('admin', 'password')
>exit

Create a new database

One other thing worth mentioning is the admin user has to authenticate against the admin database before it can login to any other db. To put this simply, if there is a database called foo and you wish to login to foo using the admin user just created above the authentication will fail. You have to authenticate against the admin database first as show below. To create a new database just type use followed by the db name, if the database doesn’t exist mongodb will create it.


# mongo
>use admin
>db.auth('admin', 'password')
>use graylog2
>exit

To add a new user to graylog2 database use db.addUser. The user will have read and write access to graylog2 db.


> use graylog2
> db.addUser('grayloguser', 'gray123')

To create read only user just add true to the definition.


> db.addUser("grayloguser", "gray123", true)

To view all the users having access on graylog2


> db.system.users.find()

Sample output from my machine

> db.system.users.find()
{ “_id” : ObjectId(“50ebe03a191981acae0becc2″), “user” : “grayuser”, “readOnly” : false, “pwd” : “c782eb4f377b435799046ddb0ce1a57e” }

To delete a user


> db.system.users.remove({"user" : "grayloguser"});

To change the password simply run the addUser command with a new password.


> db.addUser("grayloguser", "mynewpassword")

Now the database must have some tables to store data. The corresponding MongoDB terminology for a table is called collection. Collection does not have to be created prior to use, it is created automatically on first use. The “save” method is used to insert data into a collection. The corresponding MongoDB terminology for a row is called document. Lets create a collection named cars and insert two documents to it.


> use carslist
> db.cars.save({name:"audi",yearofmanufacture:2010,type:"diesel"});
> db.cars.save({name:"bmw",yearofmanufacture:2011,type:"petrol"});

To view data in our cars collection.


> db.cars.find()

Documents can also be inserted as follows.


document=({name:"ferrari",yearofmanufacture:2012,type:"diesel"})
db.cars.insert(document);

RockMongo screenshot below shows the collections and documents created. (To install RockMongo click here)
mongo06 300x176 Basic commands mongodb show tables, views, databases, create users and more.