1. Home
  2. MongoDB
  3. MongoDB Tutorial
  4. MongoDB – Create Database

MongoDB Create Database

MongoDB does not provide commands to create a database. You can use use dbName statement to select a database in the mongo shell. Use the following example:

Syntax:

> use DATABASE_NAME

Limitations

MongoDB database names cannot be empty and must have fewer than 64 characters.

Create Database

First, of the all, Use below command to select database. To create a new database make sure it not exists.

> use mydb

You have a database name selected. Now you need to insert at least one document to keep this database. As you have already executed use command above, so all the statements will be executed on above database.

> db.users.insert({ id: 1 })

Show Databases – After inserting the first record the database will be listed in show dbs command.

> show dbs

local           0.000GB
mydb            0.000GB

Also, You can run db command to find current selected database.

> db

mydb
Tags ,
Was this article helpful to you? Yes 4 No
Welcome to tecadmin tutorials website