Pages - Menu

Tuesday, September 1, 2015

The USE Command - To Create Database

'How to create Database', first question strikes in mind, before Starting to work with collection to insert or to retrieve or to perform other operation. In MongoDB, use DATABASE_NAME is used to create database. The use command will create a new database, if that database is not there otherwise it will return the existing Database.

Syntax:

Basic syntax to create Database is as follows:

use DATABASE_NAME

Example:

If you want to create a database with name like <mytestdb>, then use DATABASE_NAME statement will be like:

> use mytestdb
switched to db mytestdb

How to check currently selected Database:

To check currently selected database use command db.

> db
mytestdb

How to check databases list:

To check your databases list use the command show dbs.

>show dbs
local          0.078GB
m101         0.078GB
test            0.078GB

Note: Your newly created Database is not present in the list. To show database in the list, you need to store at least one document into it.

> db.NameList.insert({"name" : "Techspeculum"}) 
WriteResult({"nInserted" : 1})

> show dbs
local          0.078GB
m101         0.078GB
mytestdb   0.78GB
test            0.078GB


In MongoDB, default database is test. It means, if you will not create any database then collections will be stored in default database automatically i.e. test database.


No comments:

Post a Comment