Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MongoDB Create Database

MongoDB Create Database

In MongoDB, creating a database is a simple process. When you first insert data into MongoDB, if the database does not exist, MongoDB will automatically create it.

We only need to choose a database name and start inserting documents into it.

Syntax

When you use the use command to specify a database, if the database does not exist, MongoDB will automatically create it.

The syntax for creating a database in MongoDB is as follows:

use DATABASE_NAME

If the database does not exist, create it; otherwise, switch to the specified database.

Example

In the following example we create the database runoob:



> use runoob

switched to db runoob

> db

runoob

> 

After executing the use runoob command, MongoDB will create a new database named runoob. At this point, you can start creating collections and inserting documents in this database.

If you want to view all databases, you can use show dbs Command:



> show dbs

admin   0.000GB

config  0.000GB

local   0.000GB

> 

As you can see, the database runoob we just created is not in the database list. To display it, we need to insert some data into the runoob database.



> db.runoob.insertOne({"name":"Runoob Tutorial"})

WriteResult({ "nInserted" : 1 })

> show dbs

admin   0.000GB

config  0.000GB

local   0.000GB

runoob  0.000GB

Create a collection

After creating a database, you usually need to create collections in it to store documents.

A collection is similar to a table in a relational database. The following are the steps to create a collection:

Use the use command to specify the database.

Use the db.createCollection() method to create a collection.

Example

use myNewDatabase

db.

createCollection

(

"myNewCollection"

)

The above command will create a new collection named myNewCollection in the myNewDatabase database.

View the database list

To view a list of all databases in the current MongoDB instance, you can use the show dbs command:

show dbs

View the current database

To view the database currently in use, you can use the db command:

db

Drop the database

If you need to delete a database, you can use the db.dropDatabase() method:

use myDatabase

db.dropDatabase()

The above command will delete the currently used myDatabase database and all its collections.

The default database

The default database in MongoDB is test. If you do not create a new database, collections will be stored in the test database.

When you connect to a MongoDB instance through the shell, if you do not use the use command to switch to another database, the test database will be used by default.

For example, after starting a MongoDB instance and connecting to the MongoDB shell, if you start inserting documents without explicitly specifying a database, MongoDB will use the test database by default.

Example

use test

db.

myCollection

.

insertOne

(

{

name

:

"Alice"

,

age

:

30

}

)

In this example, if the test database does not exist, MongoDB will automatically create it.

It should be noted that the default database is only used in specific cases. In actual development, you usually choose a database you created to store data.

Note: In MongoDB, a collection is only created after content is inserted; that is, after creating a collection (data table), you must insert another document (record) for the collection to be actually created.

Notes