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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Nasa Nguyen
Guest
Nasa Nguyen
Robert McMahan
Guest
Robert McMahan

Very useful information. Thanks a lot :)

Roberto Andrew
Guest
Roberto Andrew
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… Read more »
Nayan
Guest
Nayan

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??

mbokil
Guest
mbokil

I use Robomongo as a GUI for mongo. Opensource.

Fahim Babar Patel
Guest
Fahim Babar Patel

+1

amine
Guest
amine

thnx, it was a very helpful tuo

aaron9ultra
Guest
aaron9ultra

Thank you, it’s very helpful.

There is typographical error.

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

should be

into the ‘users’ collection”

sidhdhant
Guest
sidhdhant

i dont know about this error

Matt Fletcher
Guest
Matt Fletcher

Brilliant and simply written guide, kinda blog posts about dev we need to see more of!

filippo
Guest
filippo

Hi man, i knew about ArangoDB (avocado), but never heard about MangoDB
Just joking, good job thanks

Marlon van der Linde
Guest
Marlon van der Linde

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

Edddie
Guest
Edddie

It is a great site you have here… I do come here often for solutions… keep up with the good work.

Edddie
Guest
Edddie

Or you can simply run a find() on the database you just switched to:
> use NewDataBase
> db.NewDataBase.find()

Matt Fletcher
Guest
Matt Fletcher

It’s borne from a bit of an odd side-effect but a good method!

tahir
Guest
tahir

Thanks for the tutorial. It really helped me starting with this wonderful db.

nandu mishra
Guest
nandu mishra

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.

Thumbi
Guest
Thumbi

great post

gaurharidass@gmail.com
Guest
gaurharidass@gmail.com

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

subhojit777
Guest
subhojit777

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.

Sridhar
Guest
Sridhar

Good poc for new bees…. thanks…

Bread
Guest
Bread

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.

Francisco
Guest
Francisco

Thanks brother, I’d that doubt and you save me

bird
Guest
bird

I begin to use this db for my work today .Thanks!