How to create database in MongoDB
MongoDB doesn’t provides any command to create “database” , and you don’t need to create it manually as well. Because, MangoDB will create it on the fly, during the first time you save the value into the defined collection, and database.
For developer from SQL backgroud, we need to create a database, then table and insert the values into the newly created table. In MongoDB, you don’t need to mention what you want to create, when first time you save the value into the defined collection, under selected database, MangoDB will create the value, collection and database automatically.
MongoDB only contains “
db.createCollection()” to create collection manually, but Not database.In this guide, we show you how and when MongoDB creates the database and collections.
1. Show all database
Use “show dbs” to display all the available databases.
MongoDB shell version: 1.8.1 connecting to: test > show dbs admin 0.03125GB local (empty)
Only two databases : “admin” and “local” available.
2. Define a database name
Issue “use new-databasename” to switch to the define database (even non-exists database name will work). However, MangoDB will not create any database yet, until you save something inside.
> use mkyongdb switched to db mkyongdb > show dbs admin 0.03125GB local (empty)
Database “mkyongdb” is not create 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 dbs admin 0.03125GB local (empty) mkyongdb 0.03125GB
This says, “save the document ‘{username:”mkyong”}’ into the ‘user’ collection”. When this “save” operation is performed, MangoDB will create the “user” collection, and “mkyongdb” database automatically.






A tutorial on the most basics of mongo… so it’s for newbies, which is fine.
… then **completely skips by** how to get into the console to use the commands in the tutorial.
A bad oversight, especially if you’re going to assume the user is new enough to mongo to warrant explaining the difference between it and traditional sql.
Thanks for your suggestion, will improve the tutorial and provide more detail steps.
Thanks brother, I’d that doubt and you save me
I begin to use this db for my work today .Thanks!