MongoDB – Create Database:
Database in MongoDB is nothing but a logical storage of all collections created. A mongoDB server can have multiple databases.
By Default, when we install the mongoDB we will have 2 databases. To check the databases below is the command.
[root@i2mongo ~]# mongo MongoDB shell version: 2.6.9 connecting to: test testset:PRIMARY> show dbs admin 0.031GB local 1.577GB
There is no command for creating database in specific for mongoDB. We need to define the database name which you want to use. Let us use a database called imongo.
testset:PRIMARY> use imongo switched to dbimongo testset:PRIMARY> show dbs admin 0.031GB local 1.577GB
Let us create a collection called students.
syntax:db.createCollection() to create collection manually
testset:PRIMARY> show collections testset:PRIMARY>db.students.save({firstname:"Maria",city:"Banglore"}) WriteResult({ "nInserted" : 1 }) testset:PRIMARY>db.students.find() { "_id" : ObjectId("592436a25273c93ad95b1099"), "firstname" : "Maria", "city" : "Banglore" }
Now, Check the database name by giving the below command.
testset:PRIMARY> show dbs admin 0.031GB imongo 0.031GB local 1.577GB
You can verify the Database you are in currently. Use the below command for the same.
testset:PRIMARY>db imongo