Main Tutorials

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 Author

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

Comments

Subscribe
Notify of
27 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Nasa Nguyen
9 years ago
Bruno
5 years ago

Typo MangoDB… 3 times 🙂

Phuc Tran
5 years ago

Thanks!

Robert McMahan
7 years ago

Very useful information. Thanks a lot 🙂

Roberto Andrew
8 years ago

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.

Nayan
8 years ago

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
8 years ago
Reply to  Nayan

I use Robomongo as a GUI for mongo. Opensource.

Fahim Babar Patel
9 years ago

+1

amine
9 years ago

thnx, it was a very helpful tuo

aaron9ultra
10 years ago

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
5 years ago
Reply to  aaron9ultra

i dont know about this error

Matt Fletcher
10 years ago

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

filippo
10 years ago

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

Marlon van der Linde
10 years ago

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
11 years ago

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

Edddie
11 years ago

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

Matt Fletcher
10 years ago
Reply to  Edddie

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

tahir
11 years ago

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

nandu mishra
11 years ago

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
11 years ago

great post

11 years ago

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
11 years ago

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
11 years ago

Good poc for new bees…. thanks…

Bread
11 years ago

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
12 years ago

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

bird
12 years ago

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