1. 各數(shù)據(jù)庫簡要介紹和區(qū)別
MongoDB 文檔型數(shù)據(jù)庫Document store枫甲,非關(guān)系型數(shù)據(jù)庫(部署簡單方便篓叶,無模式特性,支持索引使用BSON存儲數(shù)據(jù))各數(shù)據(jù)庫的Rank和詳細(xì)介紹,可參閱網(wǎng)站 DB-ENGINES
早期的32位系統(tǒng)眨唬,DB最多支持2.5G的大小,超過這范圍會出現(xiàn)很多問題好乐。
2. MongoDB 安裝和啟動
- 下載安裝
A. 首先從www.mongodb.org下載相應(yīng)的版本
B. 解壓壓縮文件并修改文件夾名稱匾竿, (Bin目錄中的文件mongodbump 備份,mongodbstat 監(jiān)控
C. 服務(wù)的啟動 mongod -dbpath=/data/db -port=27017蔚万,假如守護(hù)進(jìn)程啟動 mongod --dbpath=/data/db --fork --syslog (前面一個參數(shù)表示守護(hù)進(jìn)程方式啟動岭妖,后面一個參數(shù)表示使用系統(tǒng)日志也可以指定日志文件 --logpath=/var/mongd/) 進(jìn)程查看 lsof -i:27017 - 在線安裝(apt-get),先查詢 apt-cache search mongdb反璃,此方法較難找到安裝目錄
3.MongoDB 的客戶端使用
- 客戶端啟動 ./mongo 啟動本機(jī)昵慌,啟動其他機(jī)器 ./mongo ip:port
- 查看有哪些數(shù)據(jù)庫
show dbs; //列出所有數(shù)據(jù)庫 local默認(rèn)自動創(chuàng)建
- 列出集合
show collections; //列出所有集合;
use nodejs //使用數(shù)據(jù)庫淮蜈;數(shù)據(jù)庫和集合都無法創(chuàng)建斋攀,只有通過集合里面成功插入一行數(shù)據(jù)后才會創(chuàng)建數(shù)據(jù)庫和集合
- 創(chuàng)建一條記錄
db.users.insert({'username':'Zoey'});
db.users.insert(''username':'Lisi', 'size':'80kg'});
show dbs; //顯示新建的數(shù)據(jù)庫 nodejs
show collections //顯示集合 users
db.users.find().count(); //統(tǒng)計集合數(shù)量
- 查詢記錄
db.users.find({'username':'Lisi'}); //查詢username=Lisi的信息
db.users.find().toArray() //格式化記錄
db.users.find().Pretty //格式化記錄
- 更新記錄
db.users.update({條件}, {更新內(nèi)容}, upsert, multi) //upsert 默認(rèn)False,若更新記錄沒有,不自動插入礁芦, multi表示更新多條蜻韭,默認(rèn)只更新最前面一條悼尾。
db.users.update({'username':'Lisi'}, {$set:{'size':'E'}},{multi:true}) //更新多行
db.users.save({"_id":ObjectID(""), 'size':'E'}); //需要_id做查詢條件,所有值都指定肖方,否則會丟失
- 刪除記錄
db.users.remove({'username':'Lisi'},true) //刪除條件 后面一個參數(shù)true表示單行刪除闺魏,默認(rèn)為False 刪除所有
db.users.remove({}) //刪除所有數(shù)據(jù),不刪除索引
db.users.drop() //刪除所有文檔和索引 返回Boolean類型