MongoDB do not provide command to create new
database directly. Do not need to create Mongo database manually. It is created
when you save the documents into the
defined collection and database.
The MongoDB contains “db.createCollection()” and
is use to create collection manually but not the new database.
I’m going to share the following steps to create collections
and database.
Show
all Databases Command
When the below show query executed that it return
two databases, one is “admin” and
other is “local”.
> show
dbs
admin 0.03125GB
local (empty)
Define
a database name Command
> use my_new_db
switched
to db my_new_db
> show
dbs
admin 0.03125GB
local (empty)
When the above queries executed, the database “my_new_db” is not created yet.
Save
Command
In the last, define a collection named “customers” and save a dummy document inside
the customer’s collection. The results look like below.
> db.customers.save(
{name:"Anil"} )
> db.customers.find()
{
"_id" : ObjectId("1dhac6bfae67067io87ft3"),
"name" : "Anil" }
> show
dbs
admin 0.03125GB
local (empty)
my_new_db 0.03125GB
In the Mongodb,
when this “save” operation is performed that create the “customers” collection and “my_new_db”
database automatically!
For more detail, go to links.