MongoDB既是NoSQL數(shù)據(jù)庫
,又是內(nèi)存數(shù)據(jù)庫
,而且它是現(xiàn)在最強(qiáng)大、最流行的NoSQL數(shù)據(jù)庫。區(qū)別與別的NoSQL數(shù)據(jù)庫,MongoDB主要是基于Documents文檔(即一條JSON數(shù)據(jù))的。
MongoDB的特點(diǎn):
- NoSQL數(shù)據(jù)庫
- 內(nèi)存數(shù)據(jù)庫
- 存儲(chǔ)基于JSON或BSON
- 支持豐富的高級查詢
- 命令基于Javascript語法
- 對數(shù)據(jù)之間關(guān)系的支持比較弱
- 支持map-reduce的運(yùn)算框架
- 支持GirdFS的分布式文件系統(tǒng)
MongoDB持久化
MongoDB雖然是內(nèi)存數(shù)據(jù)庫
,但是它主要是將數(shù)據(jù)存儲(chǔ)在硬盤的,所有要操作的數(shù)據(jù)通過mmap的方式映射到內(nèi)存某個(gè)區(qū)域內(nèi)。所以相對于Redis的真·內(nèi)存數(shù)據(jù)庫
而言,MongoDB只是將大部分的操作數(shù)據(jù)存在內(nèi)存中。
Mac中,Mongodb的數(shù)據(jù)存儲(chǔ)位置默認(rèn)為:/usr/local/var/mongodb
。
里面名稱類似collection-4-3122184014923990948.wt
即為一個(gè)collection。
安裝
Ubuntu安裝:
$ sudo apt-get install mongodb-org
Mac安裝:
$ brew install mongodb
# 啟動(dòng)mongodb服務(wù)
$ brew services start mongodb
#或前端啟動(dòng)
$ mongod --config /usr/local/etc/mongod.conf
配置
MongoDB的配置文件在/etc/mongod.conf
。
常用的配置項(xiàng)有:
# 默認(rèn)端口27107
# 日志位置 /var/log/mongodb/mongod.log
Mongo Shell (Javascript)
當(dāng)我們進(jìn)入MongoDB客戶端后,實(shí)際上是進(jìn)入了一個(gè)類Javascript
語言的Shell交互環(huán)境。
也就是說,MongoDB中的很多命令,尤其是包括定義函數(shù)等高級命令,實(shí)際上都是Javascript語言。
了解了這點(diǎn),一些高級命令如Aggregation學(xué)起來就會(huì)放松很多。
常用命令
# 服務(wù)端啟動(dòng)
$ mongod /etc/mongod.conf
# 服務(wù)端啟停 (Ubuntu上)
$ sudo service mongod start
$ sudo service mongod stop
$ sudo service mongod restart
# 進(jìn)入客戶端
mongo
進(jìn)入mongo客戶端后,就進(jìn)入了shell交互頁面了。
常用的命令如下(注意mongodb區(qū)分大小寫):
# 顯示當(dāng)前數(shù)據(jù)庫
db
# 顯示所有數(shù)據(jù)庫
show databases
# 或
show dbs
# 切換數(shù)據(jù)庫
use 數(shù)據(jù)庫名
# 刪除當(dāng)前數(shù)據(jù)庫
db.dropDatabase()
集合
首先要記住,
MongoDB中,有這么幾個(gè)概念:
-
Database
數(shù)據(jù)庫:即一個(gè)NoSQL的非關(guān)系型數(shù)據(jù)庫 -
Collection
集合: 即代替?zhèn)鹘y(tǒng)"表格"概念的一個(gè)集合,收集性質(zhì)相似的一些數(shù)據(jù),如員工集合,或產(chǎn)品集合。 -
Document
文檔: 即一個(gè)標(biāo)準(zhǔn)JSON格式數(shù)據(jù)。相當(dāng)于傳統(tǒng)數(shù)據(jù)庫的一行record數(shù)據(jù)。
在關(guān)系數(shù)據(jù)庫中,是以表為一個(gè)數(shù)據(jù)集。而MongoDB中,是以Collection為一個(gè)數(shù)據(jù)集:
其中每一個(gè)配有_id
的記錄,就是一個(gè)Document
文檔。相當(dāng)于一條記錄。
創(chuàng)建集合:不用手動(dòng)創(chuàng)建集合,當(dāng)?shù)谝粭l數(shù)據(jù)插入時(shí),集合自動(dòng)就生成了。當(dāng)然,手動(dòng)創(chuàng)建也是可以的:
db.createCollection( "集合名", {各種屬性設(shè)置} )
# 如 (capped表示是否設(shè)置容量上限)
db.createCollection( "Products", {capped: true, size: 1000} )
# 查看所有集合
show collections
# 刪除集合
db.集合名稱.drop()
數(shù)據(jù)類型
在MongoDB中的一個(gè)Document,即一個(gè)JSON格式的文檔中,每個(gè)值都是要指定數(shù)據(jù)類型的。
現(xiàn)有數(shù)據(jù)類型如下:
-
Object ID
:文檔ID,相當(dāng)于傳統(tǒng)表中的表主鍵ID。- 可以自己創(chuàng)建
- 如果自己不設(shè)置,MongoDB會(huì)自動(dòng)生成一個(gè)名為
_id
的"主鍵"
String
Boolean
Integer
Double
-
Array
:列表,可以存儲(chǔ)多個(gè)值。 -
Object
:可以嵌套另一個(gè)子Document文檔,或說JSON數(shù)據(jù)。 Timestamp
Date
Null
數(shù)據(jù)操作
# 插入數(shù)據(jù)
db.集合名稱.insert( {數(shù)據(jù)} )
# 如
db.mycollection1.insert( {"name": "Jason", "age": 18} )
# 或
db.mycollection1.insert( {name: "Jason", age: 18} )
# 顯示集合中所有數(shù)據(jù)
db.集合名.find()
# 修改數(shù)據(jù) (如果不存在對應(yīng)的ID,則創(chuàng)建一條新數(shù)據(jù))
db.集合名.save( {"_id": "ID號", 數(shù)據(jù)} )
# 更新單條數(shù)據(jù)
db.集合名.update( {查詢條件}, {更新項(xiàng)目} )
# 如
db.mycollection1.update( {name:"Jason"}, {age:30} )
# 或
db.mycollection1.update( {name:"Jason"}, { $set:{age:30} } )
# 更新多條數(shù)據(jù) (使用"multi"選項(xiàng))
db.mycollection1.update( {job: "HR"}, {salary: 8000}, {multi: true} )
# 刪除單條數(shù)據(jù)
db.mycollection1.remove( {查詢}, {justOne: true} )
# 刪除多條數(shù)據(jù) ("justOne"選項(xiàng)默認(rèn)為false)
db.mycollection1.remove( {查詢} )
MongoDB數(shù)據(jù)備份和恢復(fù)
注意:MongoDB導(dǎo)出時(shí)候不是單文件,而是巨多JSON和BSON文件。
# 備份
$ mongodump -h 主機(jī)IP:端口 -d 數(shù)據(jù)庫名 -o 導(dǎo)出路徑
# 如
$ mongodump -h 192.168.1.101:27017 -d mydb1 -o /var/db/mongodb/
# 從本機(jī)恢復(fù)
$ mongorestore -d mydb1 --dir /var/db/mongodb/