MongoDB

Database

MongoDB Database Commands

 

1. list databases

> show dbs   or   > show databases

admin    (empty)
local    0.03125GB

-will not list empty databases unless we create new collection inside database with db.createCollection('users')

 

2. Switch to new database. It will switch also if such db doesn't exists.

> use my_db

switched to db my_db

 

To create database creatae at least one collection:

> db.createCollection('users')

Now files my_db.0 , my_db.1 and my_db.ns are created in /var/lib/mongodb/ directory.

When command > show dbs is performed my_db will be in the list too.

 

 

3. show current database

> db

my_db

 

4. delete database

> use my_db

> db.dropDatabase()

 

 

5. copy database

> db.copyDatabase('my_db', 'new_db')

 

6. renaming database (there's no direct command). Use this set of commands:

> use my_db

> db.copyDatabase('my_db', 'renamed_db')

> db.dropDatabase()

> show dbs