How to create database in MongoDB

MongoDB didn’t provides any command to create “database“. Actually, you don’t need to create it manually, because, MangoDB will 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 you want to create, when first time you save the value into the defined collection (table), under selected database, MangoDB will create the value, collection and database automatically.

Note
MongoDB contains “db.createCollection()” to create collection manually, but Not database.

In this guide, we will show you how and when MongoDB will create the database and collections.

1. Show all database

Issue “show dbs” to display all available databases.


MongoDB shell version: 1.8.1
connecting to: test
> show dbs
admin   0.03125GB
local   (empty)

Currently, only two databases are available – “admin” and “local“.

2. Define a database name

Issue “use new-databasename” to switch from default database to define database (even non-exists database name will work). However, MangoDB doesn’t create any database yet, until you save something inside.


> use mkyongdb
switched to db mkyongdb
> show dbs
admin   0.03125GB
local   (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 dbs
admin   0.03125GB
local   (empty)
mkyongdb        0.03125GB

This says, “save this document (value) ‘{username:"mkyong"}‘ into the ‘user’ collection”. When this “save” operation is performed, MangoDB will create the “user” collection, and “mkyongdb” database automatically.

References

  1. SQL to MongoDB Mapping chart

27 comments on “How to create database in MongoDB

  1. Nice tutorial, and recently I discovered a tool simply called DbSchema ( http://www.dbschema.com ). First of all I was impressed because they do diagrams for MongoDB.
    Second I found great an data explorer from them, where you can explore data from each collection and sub-documents in a separate window.

    Now I am dealing with virtual foreign key from them, to explore data from two collections bind referencing one the other via ObjectId’s.Look for DbSchema tool, is great for the diagrams they do for MongoDB, query builder and data explorer.Some features you may discover inside like virtual foreign keys makes the interaction really similar with relational databases, where you can place data in multiple collections and join it with ObjectId’s.I was surprised to see is possible to have diagrams for MongoDB as well, as for any relational database.

    Go for the tool DbSchema. Have a look on relational data browse and the virtual foreign keys there,
    they are a step forward in designing a database with data over multiple collections and references between them via ObjectId’s.

  2. what is the username & password for this newly created database via command promot???
    can you tell me good GUI tool for editing & viewing mongodb Database??

  3. Thank you, it’s very helpful.

    There is typographical error.

    At the last sentence,
    into the ‘user’ collection”

    should be

    into the ‘users’ collection”

  4. Thanks for taking time and effort to share the info. Just got into MongoDB from a mysql/postgres and some couchbase background. Been trying to figure out how the create on reference was working. ^5

  5. Thanks a lot for providing such a valuable information this beautiful database.
    I am completely new to this database.
    I thinks your blog information will be quite helpful to me.

    Can you please provide some information on what “oplog” is and why it is used for and how to interpret it or manipulate it.

    Thanks and Regards,
    Nandu Mishra.

  6. hi can u give examples how to implement connected tables like we have to implement database with 6 to 7 tables which are connected with each other like in sql server we have 10 tables which are connected with each other and they have ids and primary keys and i want to implement the same in mongodb how to implement those tables with connection with each other

  7. Thanks! I have also seen your other blog regarding how you can install mongodb in Ubuntu 11.04. Providing links to those blogs in every blog would make a great tutorial for mongodb beginners.

  8. 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.

Leave a Comment

Your email address will not be published. Required fields are marked *