MongoDB的資料庫操作 集合操作

2021-10-10 07:04:11 字數 1037 閱讀 2184

建立資料庫

選擇和建立資料庫,如果資料庫不存在則自動建立

> use articledb

switched to db articledb

# 資料庫建立在記憶體中

> show dbs

admin 0.000gb

config 0.000gb

local 0.000gb

在mongodb中,集合只有在內容插入後才會建立。也就是說,建立集合(資料表)後要再插入乙個文件(記錄),集合才會真正建立。

檢視當前使用的資料庫

> db

articledb

admin:從許可權角度來看,這是「root」資料庫,要是將乙個使用者新增到這個資料庫,這個使用者自動繼承所有資料庫的許可權。一些特定的伺服器端命令也只能從這個資料庫執行,比如列出所有的資料庫或者關閉伺服器。

local:這個資料永遠不會被複製,可以用來儲存限於本地單台伺服器的任意集合。

config:當mongodb用於分片設定時,config資料庫在內部使用,用於儲存分片的相關資訊。

刪除資料庫

> db.dropdatabase(

)

主要用來刪除已經持久化的資料庫。當前這個資料庫刪不刪無所謂,因為並沒有把它持久化到硬碟當中。

集合的顯式建立

建立"xiaoliu"集合

> db.createcollection(

"xiaoliu"

)

查詢集合

> show collections

xiaoliu

刪除集合

> db.xiaoliu.drop(

)true

MongoDB資料庫 集合分片操作

1 合理選擇片鍵 簡單的說就像資料庫索引一樣,根據索引將不同的文件儲存在不同分片上,這樣查詢效率也高 2 建立mongodb集群 3 分片操作 3.1 建立資料庫 如果資料庫不存在,則建立資料庫,否則切換到指定資料庫 use charge test 3.2 刪除資料庫集合 db.charge.dro...

MongoDB 資料庫操作

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

mongodb的資料庫操作

mongodb 建立資料庫的語法格式如下 usedatabase name 如果資料庫不存在,則建立資料庫,否則切換到指定資料庫。1.建立資料庫 show dbs local 0.078gb test 0.078gb use exam switched to db exam show dbs loc...