In DDS, you can use the use command to create and switch databases, the show command to display the database list and information, and the db command to manage the current database. Here are some common ways for DDS to create and manage databases:
Create database: Use the use command to create the database and switch to it:
use mydatabase
If the database does not exist, it will be created automatically. This creation is implicit, if you don't do anything after creation, you can't see the database you just created using show dbs. So after creation, a record is usually inserted into the database:
db.product.insert({"name":"washing machine"})For DDS, the implicitly created collection is created only after the content is inserted, that is, the collection is created only after a document (record) is inserted.
View all databases: Use the show dbs command to view all databases:
show dbs
Delete database: Use db.dropDatabase() to delete the database you are currently using:
db.dropDatabase()
Rename database: Use db.renameDatabase() to rename the database:
db.renameDatabase("<old name>", "<new name>")Database permission management: Assign read, write, or readWrite permissions to the database user:
db.createUser({
user: "user1",
pwd: "**********",
roles: [{ role: "read", db: "mydatabase" }]
})Switch database: Use the use command to switch to another database:
use otherdatabase
View the currently used database: Use the db command to view the currently used database:
db
When creating a database, please pay attention to the following points when naming the database:
Only letters, numbers, and underscores (_) in the ASCII character set can be used.
You cannot use period (.) or dollar sign ($) as the first character of the database name.
The database name should be lowercase and between 1 and 64 characters in length.
DDS reserves several database names, such as admin, config, and local, which cannot be used as database names.
The database name should be related to the name of the application or business domain to better manage and maintain the database.