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.
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.
MongoDB Explore and More… 2015
https://www.youtube.com/watch?v=_rAPWkRqOP4&list=UU_kJL5zl2zCVVviI8EPmUug
Typo MangoDB… 3 times 🙂
Thanks!
Very useful information. Thanks a lot 🙂
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.
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??
I use Robomongo as a GUI for mongo. Opensource.
+1
thnx, it was a very helpful tuo
Thank you, it’s very helpful.
There is typographical error.
At the last sentence,
into the ‘user’ collection”
should be
into the ‘users’ collection”
i dont know about this error
Brilliant and simply written guide, kinda blog posts about dev we need to see more of!
Hi man, i knew about ArangoDB (avocado), but never heard about MangoDB
Just joking, good job thanks
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
It is a great site you have here… I do come here often for solutions… keep up with the good work.
Or you can simply run a find() on the database you just switched to:
> use NewDataBase
> db.NewDataBase.find()
It’s borne from a bit of an odd side-effect but a good method!
Thanks for the tutorial. It really helped me starting with this wonderful db.
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.
great post
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
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.
Good poc for new bees…. thanks…
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!