首先我們先了解下什么是MongoDB ?
MongoDB 是由C++語言編寫的题造,是一個基于分布式文件存儲的開源數(shù)據(jù)庫系統(tǒng)傍菇。 在大量數(shù)據(jù)下,承載性能好界赔。
MongoDB 將數(shù)據(jù)存儲為一個文檔丢习,數(shù)據(jù)結(jié)構(gòu)由鍵值(key=>value)對組成。MongoDB 文檔類似于 JSON 對象淮悼。字段值可以包含其他文檔咐低,數(shù)組及文檔數(shù)組。
它和MySQL的區(qū)別嗎吶在于:
? ? ? ? ? ?mongodb:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 優(yōu)點(diǎn): ? ?非關(guān)系型的數(shù)據(jù)庫袜腥。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 相對于mysql來說见擦,它不需要提前創(chuàng)建表,以及表結(jié)構(gòu),為我們省? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 去了很多的不便
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 存儲持久化鲤屡,面向集合存儲儡湾;易存儲對象類型的數(shù)據(jù)。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 性能方面能夠快速查詢(?支持動態(tài)查詢执俩,完全索引徐钠。)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? json格式存儲。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?缺點(diǎn):? ?MongoDB占用空間過大?役首。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 因?yàn)樗欠顷P(guān)系型尝丐,所以關(guān)系能力比較薄弱。
? ? ? ? ?? mysql:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 優(yōu)點(diǎn): 關(guān)系型數(shù)據(jù)庫衡奥。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?不同的引擎有不同的存儲方法爹袁。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?有較為成熟健全的體系。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?缺點(diǎn):? 在海量數(shù)據(jù)進(jìn)行查詢的時候性能效率慢矮固。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?在創(chuàng)建數(shù)據(jù)庫失息,表的時候比較費(fèi)時,特別是在創(chuàng)建表的時候設(shè)定? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 的字段需要特別注意档址。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?查詢數(shù)據(jù)信息的時候盹兢,有的語法較為復(fù)雜,費(fèi)時守伸。
接下來總結(jié)性的mongodb的語法:
Linux(Ubuntu16.04)
啟動 MongoDB?:
sudo ? service ? ?mongod ? start
創(chuàng)建數(shù)據(jù)庫:
use?database_name
注:(如果數(shù)據(jù)庫不存在绎秒,則創(chuàng)建數(shù)據(jù)庫,否則切換到指定數(shù)據(jù)庫)
刪除數(shù)據(jù)庫:
db.dropDatabase()
查看所有數(shù)據(jù)庫:
show dbs
MongoDB 創(chuàng)建集合:
db.createCollection()
MongoDB 刪除集合:
db.collection.drop()
MongoDB?插入文檔:
db.COLLECTION_NAME.insert(document)
MongoDB 更新文檔:
db.collection.update()
MongoDB 刪除文檔:
db.collection.remove()
MongoDB 查詢文檔:
db.collection.find()
MongoDB AND 條件:
>db.col.find({key1:value1,?key2:value2}).pretty()
注:(MongoDB 的 find() 方法可以傳入多個鍵(key)尼摹,每個鍵(key)以逗號隔開见芹,即常規(guī) SQL 的 AND 條件)
MongoDB OR 條件:
MongoDB OR 條件語句使用了關(guān)鍵字?$or,
db.col.find({$or:[{"by":"肖申克的救贖"},{"title":?"人性/現(xiàn)實(shí)"}]}).pretty()
MongoDB?條件操作符:
作用:條件操作符用于比較兩個表達(dá)式并從mongoDB集合中獲取數(shù)據(jù)。
(>) 大于 - $gt
(<) 小于 - $lt
(>=) 大于等于 - $gte
(<= ) 小于等于 - $lte
大于操作符 - $gt:
db.col.find({likes?:?{$gt?:?100}})
大于等于操作符 - $gte:
db.col.find({likes?:?{$gte?:?100}})
小于操作符 - $lt:
db.col.find({likes?:?{$lt?:?150}})
小于操作符 - $lte:
db.col.find({likes?:?{$lte?:?150}})
使用 (小于) 和 (大于) 查詢 - $lt 和 $gt:
db.col.find({likes?:?{$lt?:200,?$gt?:?100}})
MongoDB Limit() 方法:
作用:指定從MongoDB中讀取的記錄條數(shù)
db.collection_name.find().limit(number)
MongoDB Skip() 方法:
作用:跳過指定數(shù)量的數(shù)據(jù)
db.collection_name.find().limit(number).skip(number)
MongoDB 排序sort() 方法:
作用: ?用 1 和 -1 來指定排序的方式蠢涝,其中 1 為升序排列玄呛,而 -1 是用于降序排列。
db.collection_name.find().sort({key:1(-1)})
MongoDB 索引 ?createIndex() 方法:
db.col.createIndex({"title":1})
注: ??語法中 Key 值為你要創(chuàng)建的索引字段和二,1 為指定按升序創(chuàng)建索引徘铝,如果你想按降序來創(chuàng)建索引指定為 -1。
MongoDB 聚合:
作用: ??聚合(aggregate)主要用于處理數(shù)據(jù)(諸如統(tǒng)計(jì)平均值,求和等)儿咱,并返回計(jì)算后的數(shù)據(jù)結(jié)果庭砍。有點(diǎn)類似sql語句中的 count(*)场晶。
db.collection_name.aggregate(aggregate_operation)
MongoDB 備份(mongodump)
在Mongodb中我們使用mongodump命令來備份MongoDB數(shù)據(jù)混埠。該命令可以導(dǎo)出所有數(shù)據(jù)到指定目錄中。
mongodump -h dbhost -d dbname -o dbdirectory
-h:MongDB所在服務(wù)器地址诗轻,例如:127.0.0.1钳宪,當(dāng)然也可以指定端口號:127.0.0.1:27017
-d:需要備份的數(shù)據(jù)庫實(shí)例,例如:test
-o:備份的數(shù)據(jù)存放位置,例如:c:\data\dump吏颖,當(dāng)然該目錄需要提前建立搔体,在備份完成后,系統(tǒng)自動在dump目錄下建立一個test目錄半醉,這個目錄里面存放該數(shù)據(jù)庫實(shí)例的備份數(shù)據(jù)
如果沒有開啟權(quán)限
mongodump -h 127.0.0.1:27017 -d 數(shù)據(jù)庫名稱 -o ~/Desktop/數(shù)據(jù)庫備份的路徑
mongodump -h 127.0.0.1:27017 -o ~/Desktop/數(shù)據(jù)庫備份的路徑
如果開啟了權(quán)限設(shè)置(使用超級管理員權(quán)限)
我們也可以使用賬號密碼的方式備份指定用戶的數(shù)據(jù)庫
設(shè)置了超級管理員也可以使用如下方法備份
mongodump -u username -p password --authenticationDatabase 'admin' -d dbname -o dbpath
恢復(fù)備份
mongodb使用?mongorestore?命令來恢復(fù)備份的數(shù)據(jù)疚俱。
mongorestore -h hostname:port -d dbname? --dir path
-h (host):MongoDB所在服務(wù)器地址,默認(rèn)為: localhost:27017
-d (db):需要恢復(fù)的數(shù)據(jù)庫實(shí)例缩多,例如:test呆奕,當(dāng)然這個名稱也可以和備份時候的不一樣,比如test2
--dir:指定備份的目錄
如果沒有開啟權(quán)限
mongorestore -h 127.0.0.1:27017 -d 數(shù)據(jù)庫名稱 --dir 數(shù)據(jù)庫備份文件路徑
mongorestore -h 127.0.0.1 -dir 數(shù)據(jù)庫備份文件路徑
下面是我添加了管理員權(quán)限之后的備份命令
如果開啟了權(quán)限設(shè)置(使用超級管理員權(quán)限)
mongorestore -u username -p password --authenticationDatabase 'admin' -d 數(shù)據(jù)庫名稱 --dir 數(shù)據(jù)庫備份文件路徑
MongoDB數(shù)據(jù)導(dǎo)入與導(dǎo)出
導(dǎo)出工具:mongoexport
mongoexport -d dbname -c collectionname -o file --type json/csv -f field
參數(shù)說明:
d :數(shù)據(jù)庫名
c :collection名
o :輸出的文件名
-type : 輸出的格式衬吆,默認(rèn)為json
f :輸出的字段梁钾,如果-type為csv,則需要加上-f "字段名"
示例:
導(dǎo)出json
mongoexport -d class1804 -c books -o ~/桌面/dump/books.json --type json
導(dǎo)出csv
mongoexport -d class1804 -c books -o ~/桌面/dump/books.csv --type csv -f 'by_user,likes'
數(shù)據(jù)導(dǎo)入:mongoimport
mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv
參數(shù)說明:
d:數(shù)據(jù)庫名
c:collection名
-type:導(dǎo)入的格式默認(rèn)json
f:導(dǎo)入的字段名
-headerline:如果導(dǎo)入的格式是csv逊抡,則可以使用第一行的標(biāo)題作為導(dǎo)入的字段
-file:要導(dǎo)入的文件
MongoDB 復(fù)制(副本集)
作用: ??是將數(shù)據(jù)同步在多個服務(wù)器的過程姆泻,復(fù)制還允許從硬件故障和服務(wù)中斷中恢復(fù)數(shù)據(jù)。
MongoDB復(fù)制原理
mongodb的復(fù)制至少需要兩個節(jié)點(diǎn)冒嫡。其中一個是主節(jié)點(diǎn)拇勃,負(fù)責(zé)處理客戶端請求,其余的都是從節(jié)點(diǎn)孝凌,負(fù)責(zé)復(fù)制主節(jié)點(diǎn)上的數(shù)據(jù)潜秋。
mongodb各個節(jié)點(diǎn)常見的搭配方式為:一主一從、一主多從胎许。