MongoDB Create Database
In MongoDB, creating a database is a simple process. When you insert data into MongoDB for the first time, if the database does not exist, MongoDB will automatically create it.
We only need to select a database name and start inserting documents into it.
Syntax
When you useusecommand 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 created the database example:
> use example switched to db example > db example >
After executinguse examplecommand, MongoDB will create a new database named example. At this point, you can start creating collections and inserting documents in this database.
If you want to view all databases, you can useshow dbscommand:
> show dbs admin 0.000GB config 0.000GB local 0.000GB >
You can see that the database example we just created is not in the database list. To display it, we need to insert some data into the example database.
> db.example.insertOne({"name":"Example"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
example 0.000GB
Create Collection
After creating a database, you usually need to create collections in it to store documents.
Collections are similar to tables in relational databases. Here are the steps to create a collection:
Use the use command to specify a database.
Use the db.createCollection() method to create a collection.
Example
db.createCollection("myNewCollection")
The above command will create a new collection named myNewCollection in the myNewDatabase database.
View Database List
To view the list of all databases in the current MongoDB instance, you can useshow dbscommand:
show dbs
View Current Database
To view the database currently in use, you can usedbcommand:
db
Delete Database
If you need to delete a database, you can usedb.dropDatabase()method:
use myDatabase db.dropDatabase()
The above command will delete the currently used myDatabase database and all its collections.
Default Database
The default database in MongoDB is test. If you have not created a new database, collections will be stored in the test database.
When you connect to a MongoDB instance via 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
db.myCollection.insertOne({ name: "Alice", age: 30 })
In this example, if the test database does not exist, MongoDB will automatically create it.
Note that the default database is only used in specific circumstances. In actual development, you usually choose a database you created yourself to store data.
Note:In MongoDB, a collection is created only after content is inserted. That is, after creating a collection (data table), you must insert a document (record) before the collection is actually created.
Notes
- Database names cannot contain spaces, dots (.), or dollar signs ($).
- Database creation is automatic and does not require explicit creation unless you need to specify specific configuration options at creation time.
- In MongoDB, only when there is at least one collection in the database will the database be displayed in the
show dbsoutput of the command.