This chapter introduces the basic operations of the MongoDB database, including database creation, deletion, and querying.
Display all databases
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
tizi365 0.000GB
Switch database
Syntax:
use DATABASE_NAME
Example:
> use mydb
switched to db mydb
Display current database name
Enter the db command
> db
test
Create database
You don't need to explicitly create a database. Just switch to a non-existent database using use and insert a piece of data, and the database will be created automatically. Example:
// Switch to a non-existent database
> use mydb
switched to db mydb
// Insert a document into the movie collection (the movie collection will be created automatically if it does not exist)
> db.movie.insert({"name":"tutorials point"})
// Display all databases (mydb database has been created automatically)
> show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
Note: MongoDB databases and collections do not need to be created in advance; they are automatically created when data is first written.
Delete database
The db.dropDatabase() API can be used to delete the current database. Example:
// Switch to the mydb database
> use mydb
switched to db mydb
// Delete the mydb database
> db.dropDatabase()
> { "dropped" : "mydb", "ok" : 1 }