python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
How to create database in MongoDB?

How to create database in MongoDB ?

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.

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


Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - and
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like Powered by Blogger.
ASK Questions