NoSQL是not only SQL的縮寫,它指的非關系型資料庫,是以key-value形式存儲,和傳統資料不一樣,比如不遵循sql标準。随着大資料時代的到來,NoSQL有助于解決傳統資料庫擴充困難,讀寫慢,成本高,有限的支撐容量問題.因為它在擴充和性能方面做了提升,對于事務性的要求還不能代替傳統資料庫。目前新浪微網誌的redis資料庫,視覺中國的mongodb都屬于NoSQL資料庫範疇。
<a href="http://blog.51cto.com/attachment/201209/001037252.png" target="_blank"></a>
Mongodb是一個介于關系資料庫和非關系資料之間的産品,它文法類似javascript語言。資料是鍵與值的一種組合,格式如{“age”:25}形式,資料以集合存儲。目前已超過百家網站使用mongodb。
<a href="http://blog.51cto.com/attachment/201209/001100777.png" target="_blank"></a>
首先到mongodb.org上下載下傳相應版本。
Cd mongodb-linux-x86-2.2.2 //解壓完畢後即成為可執行程式
<a href="http://blog.51cto.com/attachment/201209/001118512.png" target="_blank"></a>
Mkdir /usr/local/mongodb
Mkdir /usr/local/mongodb/data
Rsync -auvz /root/mongo-linux-ix86-2.2.0/bin/ /usr/local/mongodb/
Touch /usr/local/mongodb/dblogs
<a href="http://blog.51cto.com/attachment/201209/001146175.png" target="_blank"></a>
/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb –logpath=/usr/local/mongodb/dblogs –fork //啟動mongodb
<a href="http://blog.51cto.com/attachment/201209/001201759.png" target="_blank"></a>
Echo “/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb –logpath=/usr/local/mongodb/dblogs --fork” >>/etc/rc.local //開機啟動
<a href="http://blog.51cto.com/attachment/201209/001215153.png" target="_blank"></a>
<a href="http://blog.51cto.com/attachment/201209/001229174.png" target="_blank"></a>
一般使用pkill mongod //終止程序,避免使用kill -9殺死程序,因為容易造成mongodb死鎖。
<a href="http://blog.51cto.com/attachment/201209/001243787.png" target="_blank"></a>
/usr/local/mongodb/bin/mongo 進入字元界面
<a href="http://blog.51cto.com/attachment/201209/001259256.png" target="_blank"></a>
Mongodb資料庫不同于傳統的關系資料結構,它包括文檔,集合和資料三級結構組成。實體結構為一個ns檔案一些資料庫.0和資料庫.1組成。Mongodb資料類型,包括null、布爾、數字、字元串、數組以及對象六種資料類型組成。
<a href="http://blog.51cto.com/attachment/201209/001317457.png" target="_blank"></a>
<a href="http://blog.51cto.com/attachment/201209/001332951.png" target="_blank"></a>
>Show dbs //顯示所有資料庫
>db.dropDatabase(); //删除目前資料庫
>use mydb //建立新的資料庫
>db.user.insert({name:”user1”,age:25}); //插入資料
>db.user.find(); //檢視資料
>db.user.drop({name:”user1”}); //删除記錄
>db.user.update({_id:1},{name:”user2”}); //修改記錄
<a href="http://blog.51cto.com/attachment/201209/001402367.png" target="_blank"></a>
<a href="http://blog.51cto.com/attachment/201209/001414688.png" target="_blank"></a>
>db.user.save({name:”user2”}); //儲存資料
save和insert一般有同樣的插入效果。 但是當主鍵id相同時,save便會隻做修改不做插入的操作。
<a href="http://blog.51cto.com/attachment/201209/001428867.png" target="_blank"></a>
<a href="http://blog.51cto.com/attachment/201209/001444855.png" target="_blank"></a>
Db.cl.remove();删除所有記錄,如果加上記錄字段則删除相應字段。
<a href="http://blog.51cto.com/attachment/201209/001459130.png" target="_blank"></a>
批量插入資料
<a href="http://blog.51cto.com/attachment/201209/001516910.png" target="_blank"></a>
Js方式插入資料
<a href="http://blog.51cto.com/attachment/201209/001529869.png" target="_blank"></a>
Db.c1.find();查詢語句
<a href="http://blog.51cto.com/attachment/201209/001542263.png" target="_blank"></a>
Find指令也可以指查找指定字段,字段值為1為真顯示,為0為假。
<a href="http://blog.51cto.com/attachment/201209/001558763.png" target="_blank"></a>
條件查詢,gt大于,lt小于,e等于,ne不等于,gte大于等于,lte小于等于,用$定義。
<a href="http://blog.51cto.com/attachment/201209/001632220.png" target="_blank"></a>
Sort用于排序排列,用sort排列,-1位倒叙,1位正序排列
<a href="http://blog.51cto.com/attachment/201209/001649892.png" target="_blank"></a>
Limit用來控制顯示輸出個數,skip用來排除前面多少個
<a href="http://blog.51cto.com/attachment/201209/001705977.png" target="_blank"></a>
in用來進行範圍查找
<a href="http://blog.51cto.com/attachment/201209/001720288.png" target="_blank"></a>
$or或的查詢,$nor非,or與in差別是:or可以接不同字段,in需要接同一個字段。
<a href="http://blog.51cto.com/attachment/201209/001732979.png" target="_blank"></a>
插入數組,用elemMatch查找數組,代替post.title
<a href="http://blog.51cto.com/attachment/201209/001757801.png" target="_blank"></a>
查詢控制兩種方法
<a href="http://blog.51cto.com/attachment/201209/002346468.png" target="_blank"></a>
查詢數組記錄,必須在第二個字段進行
<a href="http://blog.51cto.com/attachment/201209/001819605.png" target="_blank"></a>
Db.collection.update(criteria,objNew,upsert,multi);
Criteria:用于設定查詢條件的對象
Objnew:用于設定更新内容
Upsert:如果記錄村子啊就更新它,否則新增一記錄
Multi:如果多個符合條件的記錄,隻會更新第一個符合條件的記錄
<a href="http://blog.51cto.com/attachment/201209/001835644.png" target="_blank"></a>
Upsert第三個參數,意思是如果字段不存在則增加該字段
<a href="http://blog.51cto.com/attachment/201209/002131511.png" target="_blank"></a>
$set可以用于改變源字段值或增加新字段
<a href="http://blog.51cto.com/attachment/201209/002001605.png" target="_blank"></a>
$inc字段每個年齡增加一歲,inc對字段曾進行增減值的更改
<a href="http://blog.51cto.com/attachment/201209/001921816.png" target="_blank"></a>
$unset删除字段,當age為真時删除字段
<a href="http://blog.51cto.com/attachment/201209/002021319.png" target="_blank"></a>
$push增加數組字段
<a href="http://blog.51cto.com/attachment/201209/002041676.png" target="_blank"></a>
$pop删除上條記錄,如為-1則從前數,$pushAll一次插入多條記錄
<a href="http://blog.51cto.com/attachment/201209/002410610.png" target="_blank"></a>
addToSet隻能插入不同值記錄
<a href="http://blog.51cto.com/attachment/201209/002429965.png" target="_blank"></a>
$addToSet和$each一起用一次加入多個值
<a href="http://blog.51cto.com/attachment/201209/002441217.png" target="_blank"></a>
$pull删除數組指定值,$pullAll删除多個指定值
<a href="http://blog.51cto.com/attachment/201209/002454763.png" target="_blank"></a>
$rename更改字段名
<a href="http://blog.51cto.com/attachment/201209/002509379.png" target="_blank"></a>
固定集合是固定大小的資料存儲方式,它速度處理更快
Db.createCollection(c6,{capped:true,size:10000,max:5}); //資料大小10kB,記錄5條,填滿了則取代前面的記錄
Db.runCommand({convertTocapped:”test”,size:10000});
<a href="http://blog.51cto.com/attachment/201209/002534573.png" target="_blank"></a>
GridFS是一種大二進制檔案,可以存儲一些大檔案。包含.files中繼資料對象和.chunks二進制塊資訊。
加載GridFS資料類型:
Cd /usr/local/mongodb/bin
Tar zcvf mongofiles.tar.gz mongosniff
./mongofiles put mongofiles.tar.gz
<a href="http://blog.51cto.com/attachment/201209/002546626.png" target="_blank"></a>
Mongofile是進行檔案上傳下載下傳的工具,put上傳,get下載下傳
<a href="http://blog.51cto.com/attachment/201209/002600889.png" target="_blank"></a>
性能優化,如同大多數資料庫一樣索引可以很好地完成優化工作。可以通過explain()檢視資料查詢記錄的次數。
<a href="http://blog.51cto.com/attachment/201209/002614141.png" target="_blank"></a>
>db.c1.ensureIndex({name:1}); //建立索引
>db.c1.getIndexes(); //檢視索引
<a href="http://blog.51cto.com/attachment/201209/002626300.png" target="_blank"></a>
再執行explain()便可以看到次數變為8->1
<a href="http://blog.51cto.com/attachment/201209/002639864.png" target="_blank"></a>
Db.c1.dropIndexes(); //删除索引
<a href="http://blog.51cto.com/attachment/201209/002654115.png" target="_blank"></a>
profile是一種慢查詢日志功能。可以通過兩種方式開啟:
1、 啟動mongodb是可以加上—profile=級别
2、 使用db.setProfilingLevel(級别,時間); 定義級别
0-:不開啟;1-:記錄慢查詢;2-:記錄所有密令
最後通過>db.getProfilingLevel();檢視級别
<a href="http://blog.51cto.com/attachment/201209/002709435.png" target="_blank"></a>
首先建立索引可以提高執行效率
其次使用limit();可以減少資源的消耗量
再次capped collection固定空間功能可以提高效率
再次,查詢時指定字段而不是所有字段,提高速度
最後使用慢查詢功能,優化語句。
mongosniff指令可以監控資料庫操作曆史
<a href="http://blog.51cto.com/attachment/201209/002722129.png" target="_blank"></a>
Mongostat指令可以檢視mongodb實時結果
<a href="http://blog.51cto.com/attachment/201209/003216574.png" target="_blank"></a>
本文轉自zsaisai 51CTO部落格,原文連結:http://blog.51cto.com/3402313/989251