MongoDB didn ' t provides any command to create "database". Actually, you don ' t need to create it manually, because, Mangodb'll create it on the fly, during the first time you save The value into the defined collection (or table in SQL), and database.
For developer from SQL background, we need to create a database, table and insert values into table manually. In MongoDB, you don ' t need to mention "What do you want to create, when first time you save the value into the defined collect Ion (table), under selected database, Mangodb would create the value, collection and database automatically.
Note
MongoDB contains "
db.createCollection()
"To create collection manually, and not database.
In this guide, we'll show you what and when MongoDB would create the database and collections.
1. Show All database
Issue "to the show dbs
display all available databases.
MongoDB Shell version:1.8.1connecting to:test> show Dbsadmin 0.03125GBlocal (empty)
Currently, only databases is available– "admin" and "local".
2. Define a database name
Issue "To switch from the use new-databasename
default database to define database (even non-exists database name would work). However, Mangodb doesn ' t create any database yet, until you save something inside.
> Use mkyongdbswitched to DB mkyongdb> show Dbsadmin 0.03125GBlocal (empty)
P.S Database "mkyongdb" is not created yet.
3. Save It
Define A collection named "Users", and save a dummy document (value) inside.
> db.users.save ({username: "Mkyong"})> Db.users.find () {"_id": ObjectId ("4dbac7bfea37068bd0987573"), "username": "Mkyong"}>> show Dbsadmin 0.03125GBlocal (empty)mkyongdb 0.03125GB
This says, "Save this document (value) ' To {username:"mkyong"}
the ' user ' collection". When this "save" operation is performed, Mangodb'll create the "user" collection, and "mkyongdb" Datab ASE automatically.
How to create a MongoDB database