Create the database in MongoDB

Create the database in MongoDB

1. Connect with MonoDB
Whenever you connect with Mongodb, it will go to default test database

D:\mongodb\bin>mongo
MongoDB shell version: 2.6.4
connecting to: test
> db
test

2. Display the list of databases

> show dbs
admin  (empty)
local  0.078GB
test   (empty)

3. Switch to a new database named mydb

>use mydb
switched to db mydb

4. Check the database.

>db
mydb

Note: If you issue the show dbs operation again,it will not include the mydb database.
MongoDB will not permanently create a database until you insert data into that database.

Example:

-- Assign value to j
>j={name:"RAM"}
{"name" : "RAM" }

--Insert value into table employee
>db.employee.insert(j)
WriteResult({ "nInserted" : 1 })

-- Fire select query on employee table
>db.employee.find()
{ "_id" : ObjectId("540eb24efbf4b1a1447adde9"), "name" : "RAM" }

-- Check the databases
>show dbs
admin  (empty)
local  0.078GB
mydb   0.078GB
test   (empty)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.