3.9.1. Grammar ¶
The syntax format for MongoDB to create a database is as follows:
use DATABASE_NAME
If the database does not exist, create the database, otherwise switch to the specified database.
3.9.2. Example ¶
For the following example, we created the database runoob:
> use runoob
switched to db runoob
> db
runoob
>
If you want to view all databases, you can use the show dbs Command:
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
>
As you can see, the database we just created, runoob, is not in the database list. To display it, we need to insert some data into the runoob database.
> db.runoob.insert({"name":"菜鸟教程"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
runoob 0.000GB
The default database in MongoDB is test. If you do not create a new database, the collection will be stored in the test database.
注意: In MongoDB, collections are created only after content has been inserted! That is, after the collection (data table) is created, another document (record) is inserted before the collection is really created.