MongoDB知識整理

爲什麼我們要使用MongoDB?

特點:

  • 高性能、易部署、易使用,存儲數據非常方便。主要功能特性有:
  • 面向集合存儲,易存儲對象類型的數據。
  • 模式自由。
  • 支持動態查詢。
  • 支持完全索引,包含內部對象。
  • 支持查詢。
  • 支持複製和故障恢復。
  • 使用高效的二進制數據存儲,包括大型對象(如視頻等)。
  • 自動處理碎片,以支持雲計算層次的擴展性
  • 支持Python,PHP,Ruby,Java,C,C#,Javascript,Perl及C++語言的驅動程序,社區中也提供了對Erlang及.NET等平臺的驅動程序。
  • 文件存儲格式爲BSON(一種JSON的擴展)。
  • 可通過網絡訪問。

功能:

  • 面向集合的存儲:適合存儲對象及JSON形式的數據。
  • 動態查詢:Mongo支持豐富的查詢表達式。查詢指令使用JSON形式的標記,可輕易查詢文檔中內嵌的對象及數組。
  • 完整的索引支持:包括文檔內嵌對象及數組。Mongo的查詢優化器會分析查詢表達式,並生成一個高效的查詢計劃。
  • 查詢監視:Mongo包含一個監視工具用於分析數據庫操作的性能。
  • 複製及自動故障轉移:Mongo數據庫支持服務器之間的數據複製,支持主-從模式及服務器之間的相互複製。複製的主要目標是提供冗餘及自動故障轉移。
  • 高效的傳統存儲方式:支持二進制數據及大型對象(如照片或圖片)
  • 自動分片以支持雲級別的伸縮性:自動分片功能支持水平的數據庫集羣,可動態添加額外的機器。

適用場合:

  • 網站數據:Mongo非常適合實時的插入,更新與查詢,並具備網站實時數據存儲所需的複製及高度伸縮性。
  • 緩存:由於性能很高,Mongo也適合作爲信息基礎設施的緩存層。在系統重啓之後,由Mongo搭建的持久化緩存層可以避免下層的數據源 過載。
  • 大尺寸,低價值的數據:使用傳統的關係型數據庫存儲一些數據時可能會比較昂貴,在此之前,很多時候程序員往往會選擇傳統的文件進行存儲。
  • 高伸縮性的場景:Mongo非常適合由數十或數百臺服務器組成的數據庫。Mongo的路線圖中已經包含對MapReduce引擎的內置支持。
  • 用於對象及JSON數據的存儲:Mongo的BSON數據格式非常適合文檔化格式的存儲及查詢。

 

 

MongoDB要注意的問題

1 因爲MongoDB是全索引的,所以它直接把索引放在內存中,因此最多支持2.5G的數據。如果是64位的會更多。

2 因爲沒有恢復機制,因此要做好數據備份

3 因爲默認監聽地址是127.0.0.1,因此要進行身份驗證,否則不夠安全;如果是自己使用,建議配置成localhost主機名

4 通過GetLastError確保變更。(這個不懂,實際中沒用過

 

MongoDB結構介紹

MongoDB中存儲的對象時BSON,是一種類似JSON的二進制文件,它是由許多的鍵值對組成。如下所示

複製代碼
{  
"name" : "huangz",  
"age" : 20,  
"sex" : "male"  
}  
{    
"name" : "jack",  
"class" : 3,  
 "grade" : 3  
} 
複製代碼

而數據庫的整體結構組成如下:

鍵值對--》文檔--》集合--》數據庫

MongoDB的文件單個大小不超過4M,但是新版本後可提升到16M

MongoDB中的key命名規則如下:

  • "\0"不能使用
  • 帶有"."號,"_"號和"$"號前綴的Key被保留
  • 大小寫有區別,Age不同於age
  • 同一個文檔不能有相同的Key
  • 除了上面幾條規則外,其他所有UTF-8字符都可以使用

常用命令

1 #進入數據庫

use admin

2 #增加或修改密碼

db.addUser('xingoo','123')

db.addUser("xingoo","123",true) 參數分別爲 用戶名、密碼、是否只讀

3 #查看用戶列表

db.system.users.find()

4 #用戶認證

db.auth('xingoo','123')

5 #刪除用戶

db.removeUser('xingoo')

6 #查看所有用戶

show users

7 #查看所有數據庫

show dbs

8 #查看所有的collection集合

show collections

9 #查看各個collection的狀態

db.printCollectionStats()

10 #查看主從複製狀態

db.printReplicationInfo()

11 #修復數據庫

db.repairDatabase()

12 #設置profiling,0:off 1:slow 2 all

db.setProfilingLevel(1)

13 #查看profiling

show profiling

14 #拷貝數據庫

db.copyDatabase('xingootest','xingootest1')

db.copyDatabase("xingootest","temp","127.0.0.1")

15 #刪除集合collection

db.xingootest.drop()

16 #刪除當前數據庫

db.dropDatabase()

 

MongoDB增刪改命令

1 #存儲嵌套的對象

db.foo.save({'name':xingoo,'age':25,'address':{'city':'changchun','Province':'Jilin'}})

2 #存儲數組對象

db.foo.save({'name':xingoo,'age':25,'address':['Jilin Province','Liaoning Province']})

3 #根據query條件修改,如果不存在則插入,允許修改多條記錄

db.foo.update({'age':'25'},{'$set':{'name':'xingoo'}},upsert=true,multi=true)

4 #刪除yy=5的記錄

db.foo.remove({'name':'xingoo'})

5 #刪除所有的記錄

db.foo.remove()

 

索引

1 #增加索引:1 asc -1 desc

db.foo.ensureIndex({firstname:1,lastname:-1},{unieap:true})

2 #索引子對象(不懂)

db.foo.ensureIndex({'Al.Em':!})

3 #查看索引信息

db.foo.getIndexes()

db.foo.getIndexKeys()

4 #根據索引名刪除索引(不懂)

db.foo.dropIndex('Al.Em_1')

 

查詢

條件操作符

複製代碼
 1 $gt ---- >
 2 $lt ---- <
 3 $gte ---- >=
 4 $lte ---- <=
 5 $ne ---- != 、<>
 6 $in ---- in
 7 $nin ---- not in
 8 $all ---- all
 9 $or ---- or
10 $not ---- 反匹配
複製代碼

 

1 #查詢所有記錄

db.foo.find() ---- select * from foo

2 #查詢某列非重複的記錄

db.foo.distinct("xingoo") ---- select distinct name from foo

3 #查詢age = 22 的記錄

db.foo.find({"age":22}) ---- select * from foo where age = 22

4 #查詢age > 22 的記錄

db.foo.find({age:{$gt:22}}) ---- select * from foo where age > 22

5 #查詢age < 22 的記錄

db.foo.find({age:{$lt:22}}) ---- select * from foo where age < 22

6 #查詢age <= 25的記錄

db.foo.find({age:{$lte:25}})

7 #查詢age >= 23 並且 age <=26的記錄

db.foo.find({age:{gte:23,lte:26}})

8 #查詢name中包含xingoo的數據

db.foo.find({name:/xingoo/}) ---- select * from foo where name like '%xingoo%'

9 #查詢name中以xingoo開頭的數據

db.foo.find({name:/^xingoo/}) ---- select * from foo where name like 'xingoo%'

10 #查詢指定列name、age的數據

db.foo.find({},{name:1,age:1}) ---- select name,age from foo

11 #查詢制定列name、age數據,並且age > 22

db.foo.find({age:{$gt:22}},{name:1,age:1}) ---- select name,age from foo where age >22

12 #按照年齡排序

升序:db.foo.find().sort({age:1})  降序:db.foo.find().sort({age:-1})

13 #查詢name=xingoo.age=25的數據

db.foo.find({name:'xingoo',age:22}) ---- select * from foo where name='xingoo' and age ='25'

14#查詢前5條數據

db.foo.find().limit(5) ---- select top 5 * from foo

15 #查詢10條以後的數據

db.foo.find().skip(10) ---- select * from foo where id not in (select top 10 * from foo);

16 #查詢在5-10之間的數據

db.foo.find().limit(10).skip(5) 

17 #or與查詢

db.foo.find({$or:[{age:22},{age:25}]}) ---- select * from foo where age=22 or age =25

18 #查詢第一條數據

db.foo.findOne() 、db.foo.find().limit(1)---- select top 1 * from foo

19 #查詢某個結果集的記錄條數

db.foo.find({age:{$gte:25}}).count() ---- select count(*) from foo where age >= 20

20 #按照某列進行排序(不懂)

db.foo.find({sex:{$exists:true}}).count() ---- select count(sex) from foo

21 #查詢age取模10等於0的數據

db.foo.find('this.age % 10 == 0')、db.foo.find({age:{$mod:[10,0]}})

22 #匹配所有

db.foo.find({age:{$all:[22,25]}})

23 #查詢不匹配name=X*帶頭的記錄

db.foo.find({name:{$not:/^X.*/}})

24 #排除返回age字段

db.foo.find({name:'xingoo'},{age:0})

25 #判斷字段是否存在

db.foo.find({name:{$exists:true}})

 

管理

1 #查看collection數據大小

db.xingootest.dataSize()

2 #查看collection狀態

db.xingootest.stats()

3 #查詢所有索引的大小

db.xingootest.totalIndexSize()

參考資料:

【MongoDB介紹及安裝】http://database.51cto.com/art/201103/247882.htm

【MongoDB使用入門】http://www.linuxidc.com/Linux/2013-01/78251.htm

發佈了60 篇原創文章 · 獲贊 7 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章