Wednesday 18 February 2015

mongodb : Create database

Connect to a database using 'use {database_name}' command. Even the database doesn't exist, you can connect to it using this command. Database is created on the fly once you start inserting data into it.

 

> use sample
switched to db sample
>
> show dbs
admin  (empty)
local  0.078GB
test   (empty)
>
> db.employee.insert({"id":1,"firstName":"Hari Krishna","lastName":"Gurram"})
WriteResult({ "nInserted" : 1 })
>
> show dbs
admin   (empty)
local   0.078GB
sample  0.078GB
test    (empty)

Above snippet creates database “sample”.

“show dbs” 
“show dbs” command displays all databases in the system. To display a database, it should contain at least one document. So when I call first time, it don’t display database “sample”.

db.employee.insert({"id":1,"firstName":"Hari Krishna", "lastName":"Gurram"}) 
Above statement insert a document into employee collection.

Note: 
 “test” is the default database in mongoDB. If you don’t create any database, then all the collections are stored into database “test”.


 
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment