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” 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”.
Above statement insert a document into employee collection.
“test” is the default database in mongoDB. If you don’t create any database, then all the collections are stored into database “test”.
No comments:
Post a Comment