共計(jì) 2390 個(gè)字符,預(yù)計(jì)需要花費(fèi) 6 分鐘才能閱讀完成。
這篇文章主要講解了“MongoDB 常用的基本操作命令”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著丸趣 TV 小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“MongoDB 常用的基本操作命令”吧!
在 MongoDB 數(shù)據(jù)庫(kù)里面是存在有數(shù)據(jù)庫(kù)的概念的,但是沒有模式(所有的信息都是按照文檔保存的),文檔的結(jié)構(gòu)就是 JSON 結(jié)構(gòu),只不過(guò)在進(jìn)行一些數(shù)據(jù)處理的時(shí)候才會(huì)使用到 MongoDB 自己的一些操作符。
1、切換到 stone 數(shù)據(jù)庫(kù):
db
test
執(zhí)行 db 命令可以顯示當(dāng)前數(shù)據(jù)庫(kù)對(duì)象
use stone
switched to db stone
show databases;
admin 0.000GB
local 0.000GB
實(shí)際上這個(gè)時(shí)候并不會(huì)創(chuàng)建數(shù)據(jù)庫(kù),只有在數(shù)據(jù)庫(kù)里面保存集合數(shù)據(jù)之后才能夠真正創(chuàng)建數(shù)據(jù)庫(kù)。
● admin:從權(quán)限的角度來(lái)看,這是 root 數(shù)據(jù)庫(kù)。要是將一個(gè)用戶添加到這個(gè)數(shù)據(jù)庫(kù),這個(gè)用戶自動(dòng)繼承所有數(shù)據(jù)庫(kù)的權(quán)限。一些特定的服務(wù)器端命令也只能從這個(gè)數(shù)據(jù)庫(kù)運(yùn)行,比如列出所有的數(shù)據(jù)庫(kù)或者關(guān)閉服務(wù)器。
● local: 這個(gè)數(shù)據(jù)永遠(yuǎn)不會(huì)被復(fù)制,可以用來(lái)存儲(chǔ)限于本地單臺(tái)服務(wù)器的任意集合
● config: 當(dāng) Mongo 用于分片設(shè)置時(shí),config 數(shù)據(jù)庫(kù)在內(nèi)部使用,用于保存分片的相關(guān)信息。
2、創(chuàng)建一個(gè)集合:
db.createCollection(emp
{ok : 1}
show databases;
admin 0.000GB
local 0.000GB
stone 0.000GB
這個(gè)時(shí)候 stone 數(shù)據(jù)庫(kù)才會(huì)真正存在。
3、但是很多時(shí)候如果按照以上的代碼形式進(jìn)行會(huì)覺得你不正常,因?yàn)檎H耸褂?MongoDB 數(shù)據(jù)庫(kù)集合操作的時(shí)候都是直接向里面保存一個(gè)數(shù)據(jù)。
db.dept.insert({deptno :10, dname : 財(cái)務(wù)部 , loc : 北京})
WriteResult({nInserted : 1})
4、查看所有集合
show collections;
dept
emp
發(fā)現(xiàn) dept 集合自動(dòng)創(chuàng)建。
5、查看 emp 表的數(shù)據(jù)
語(yǔ)法:db. 集合名稱.find({若干條件})
db.dept.find();
{_id : ObjectId( 59904d44d31a95e93db0da1c), deptno : 10, dname : 財(cái)務(wù)部 , loc : 北京 }
從傳統(tǒng)的數(shù)據(jù)表來(lái)看(集合就相當(dāng)于表的結(jié)構(gòu)),表的結(jié)構(gòu)一旦定義就必須按照其定義的要求進(jìn)行內(nèi)容的編寫。但是 MongoDB 不一樣,它可以自己隨意擴(kuò)充數(shù)據(jù)。
6、增加不規(guī)則的數(shù)據(jù)
var deptData={
… deptno :20,
… dname : 研發(fā)部 ,
… loc : 深圳 ,
… count :20,
… avg :8000
… };
db.dept.insert(deptData);
WriteResult({nInserted : 1})
db.dept.find()
{_id : ObjectId( 59904d44d31a95e93db0da1c), deptno : 10, dname : 財(cái)務(wù)部 , loc : 北京 }
{_id : ObjectId( 59904f2dd31a95e93db0da1d), deptno : 20, dname : 研發(fā)部 , loc : 深圳 , count : 20, avg : 8000 }
此時(shí) dept 集合的內(nèi)容可以由用戶隨便定義,完全不用考慮其他的結(jié)構(gòu),那么實(shí)際上就必須明確一點(diǎn)了,在 MongoDB 數(shù)據(jù)庫(kù)之中是絕對(duì)不可能存在有查看集合結(jié)構(gòu)的操作。
7、關(guān)于 ID 的問題
在 MongoDB 集合在的每一行記錄都會(huì)自動(dòng)的生成一個(gè):“_id : ObjectId(59904f2dd31a95e93db0da1d)”數(shù)據(jù),這個(gè)數(shù)據(jù)組成:“時(shí)間戳 + 機(jī)器碼 + 進(jìn)程 PID + 計(jì)數(shù)器”,這個(gè) ID 的信息是 MongoDB 數(shù)據(jù)庫(kù)自己為用戶生成的。
8、查看單獨(dú)的一個(gè)文檔信息
db.dept.findOne()
{
_id : ObjectId(59904d44d31a95e93db0da1c),
deptno : 10,
dname : 財(cái)務(wù)部 ,
loc : 北京
}
9、刪除數(shù)據(jù)
db.dept.remove({_id : ObjectId( 59904d44d31a95e93db0da1c)});
WriteResult({nRemoved : 1})
db.dept.find();
{_id : ObjectId( 59904f2dd31a95e93db0da1d), deptno : 20, dname : 研發(fā)部 , loc : 深圳 , count : 20, avg : 8000 }
10、更新數(shù)據(jù)
var deptData={
… deptno :30,
… dname : IT ,
… loc : 北京
… };
db.dept.update({_id : ObjectId( 59904f2dd31a95e93db0da1d)},deptData);
WriteResult({nMatched : 1, nUpserted : 0, nModified : 1})
db.dept.find();
{_id : ObjectId( 59904f2dd31a95e93db0da1d), deptno : 30, dname : IT , loc : 北京 }
11、刪除集合
語(yǔ)法:db. 集合名稱.drop();
db.dept.drop();
true
show collections;
emp
12、刪除數(shù)據(jù)庫(kù)(刪除當(dāng)前所在的數(shù)據(jù)庫(kù))
db.dropDatabase();
{dropped : stone , ok : 1}
show dbs;
admin 0.000GB
local 0.000GB
刪除數(shù)據(jù)庫(kù)是刪除當(dāng)前所在的數(shù)據(jù)庫(kù),必須先切換到數(shù)據(jù)庫(kù)后才可以刪除。
感謝各位的閱讀,以上就是“MongoDB 常用的基本操作命令”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì) MongoDB 常用的基本操作命令這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!