MongoDB 用 use + 數(shù)據(jù)庫名稱 的方式來創(chuàng)建數(shù)據(jù)庫。use 會創(chuàng)建一個新的數(shù)據(jù)庫,如果該數(shù)據(jù)庫存在,則返回這個數(shù)據(jù)庫。
use 語句的基本格式如下:
use DATABASE_NAME
創(chuàng)建一個名為 use 語句如下:
>use mydb
switched to db mydb
使用命令 db 檢查當前選定的數(shù)據(jù)庫。
>db
mydb
使用命令 show dbs 來檢查數(shù)據(jù)庫列表。
>show dbs
local 0.78125GB
test 0.23012GB
剛創(chuàng)建的數(shù)據(jù)庫(mydb)沒有出現(xiàn)在列表中。為了讓數(shù)據(jù)庫顯示出來,至少應該插入一個文檔。
>db.movie.insert({"name":"tutorials point"})
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
在 MongoDB 中,默認的數(shù)據(jù)庫是 test,如果你沒有創(chuàng)建任何數(shù)據(jù)庫,那么集合就會保存在 test 數(shù)據(jù)庫中。