mongodb的資料庫操作

2021-09-07 17:25:24 字數 1256 閱讀 3454

mongodb 建立資料庫的語法格式如下:

usedatabase_name

如果資料庫不存在,則建立資料庫,否則切換到指定資料庫。

1.建立資料庫

>show dbs

local

0.078gb

test

0.078gb

>use exam

switched to db exam

>show dbs

local

0.078gb

test

0.078gb

可以看到,我們剛建立的資料庫 exam並不在資料庫的列表中, 要顯示它,我們需要向 exam資料庫插入一些資料。

> db.exam.insert()

writeresult()

>show dbs

exam

0.078gb

local

0.078gb

test

0.078gb

mongodb 中預設的資料庫為 test,如果你沒有建立新的資料庫,集合將存放在 test 資料庫中。

mongodb 刪除資料庫的語法格式如下:

db.dropdatabase()

刪除當前資料庫,預設為 test,你可以使用 db 命令檢視當前資料庫名。(如果你刪除哪個資料庫先切換到那個資料庫再刪除)

>db

exam

>show dbs

exam

0.078gb

local

0.078gb

test

0.078gb

>db.dropdatabase()

2018-03-22t20:43:17.387+0800 e query typeerror: property '

dropdatabase

' of object exam is

not a function

at (shell):

1:4>db.dropdatabase()

>db

exam

>show dbs

local

0.078gb

test

0.078gb

集合刪除語法格式如下:

db.collection.drop()

MongoDB 資料庫操作

1 插入記錄 使用資料庫 如果沒有該資料庫就會建立,有就進入該模式 use use my testdb 插入資料 db.user.insert db.user.insert 顯示資料庫 show dbs my mongodb是隱式建立的資料庫 顯示表 show collections user是隱式...

MongoDB的資料庫操作 集合操作

建立資料庫 選擇和建立資料庫,如果資料庫不存在則自動建立 use articledb switched to db articledb 資料庫建立在記憶體中 show dbs admin 0.000gb config 0.000gb local 0.000gb在mongodb中,集合只有在內容插入後...

java 操作MongoDB資料庫

建立了乙個mongodb的資料庫連線物件,它預設連線到當前機器的localhost位址,埠是27017 mongo mongo new mongo mongo mongo new mongo 127.0.0.1 27017 for string name mongo.getdatabasenames...