Feature news

02. mongoDB - Create Database & Collection



# MongoDB use <database name> is used to create database

>use uni
switched to db uni

db is used to find the current database name

>db
uni

#show dbs is used to check your database list

>show dbs
local   0.078125GB

# Your created database (uni) is not present in list. To display database you    need to insert atleast one collection into it.

# mongoDB db.createCollection(name,{options}) is used to  create collection

options::
cappedBoolean(Optional) If true, enables a capped collection. Capped collection is a collection fixed size collecction that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify size parameter also.
autoIndexIDBoolean(Optional) If true, automatically create index on _id field.s Default value is false.
sizenumber(Optional) Specifies a maximum size in bytes for a capped collection. If If capped is true, then you need to specify this field also.
maxnumber(Optional) Specifies the maximum number of documents allowed in the capped collection.

>db.createCollection("students", { capped : true, size : 5242880, max : 5000 } )
{ "ok" : 1}

or

>db.createCollection("students")
{ "ok" : 1}
>db.createCollection("workers")
{ "ok" : 1}
>db.createCollection("lecturer")
{ "ok" : 1}

show collections is used to check your collection list

>show dbs
local   0.078125GB
uni     0.203125GB
>use uni
switched to db uni
>db
uni
>show collections
student
workers
lecturer
system.indexes

# if you need to remove the collection use this code  db.<collection name>.drop()

>db.workers.drop()
true
>show collections
student
lecturer
system.indexes
>db.lecturer.drop()
true
>show collections
student
system.indexes

# if you need delete to current database use this code  db.dropDatabase() 

>show dbs
local   0.078125GB
uni     0.203125GB
>db
uni
>db.dropDatabase()
{ "dropped" : "uni", "ok" : 1 }
>show dbs
local   0.078125GB


Next ===> mongoDB - CRUD operations

0 comments: