下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、 HYPERLINK /stoger/article/details/6633717 o MongoDB如何批量刪除集合 MongoDB如何批量刪除集合2011-07-26 09:263人閱讀 HYPERLINK /stoger/article/details/6633717 l comments 評(píng)論(0) HYPERLINK javascript:collectArticle(MongoDB%E5%A6%82%E4%BD%95%E6%89%B9%E9%87%8F%E5%88%A0%E9%99%A4%E9%9B%86%E5%90%88,6633717) 收藏 HYPERLINK /stoge
2、r/article/details/6633717 l report 舉報(bào)MongoDB的Java驅(qū)動(dòng)并沒有提供刪除集合的方法,只有單個(gè)刪除方法: DB.collection.drop();一次方法調(diào)用就是一個(gè)網(wǎng)絡(luò)通訊,那么如果需要?jiǎng)h除多個(gè)集合,需要多次調(diào)用DB.collection.drop();造成多次網(wǎng)絡(luò)通訊。幸運(yùn)的是,MongoDB提供了服務(wù)端腳本,可以通過以下腳本來批量刪除,但我覺得MongoDB應(yīng)該直接提供批量刪除功能,普通開發(fā)人員并不會(huì)意識(shí)到通訊的性能開銷。 HYPERLINK /stoger/article/details/6633717 o view plain view p
3、lainStringnames=.;/要?jiǎng)h除的集合名稱Stringcode=;for(inti=0;inames.length;i+)code+=db.getCollection(+namesi+).drop();db.eval(code);db.eval()方法用來執(zhí)行javascript腳本。需要注意的是eval執(zhí)行比較好性能,會(huì)阻塞其他操作,所以,應(yīng)區(qū)分只有一個(gè)集合的情況,不要eval。 HYPERLINK /cxd4321/archive/2011/06/24/2089052.html MongoDB數(shù)據(jù)庫文檔說明 MongoDB數(shù)據(jù)庫簡(jiǎn)單介紹MongoDB是一個(gè)高性能 ,開源 ,無模
4、式的文檔型數(shù)據(jù)庫,它在許多場(chǎng)景下可用于替代傳統(tǒng)的關(guān)系型數(shù)據(jù)庫或鍵/值存儲(chǔ)模式。MongoDB是用C+開發(fā), 提供了以下功能:面向集合的存儲(chǔ):適合存儲(chǔ)對(duì)象及JSON形式的數(shù)據(jù)。 動(dòng)態(tài)查詢:Mongo支持豐富的查詢表達(dá)式。查詢指令使用JSON形式的 標(biāo)記,可輕易查詢文檔中內(nèi)嵌的對(duì)象及數(shù)組。 完整的索引支持:包括文檔內(nèi)嵌對(duì)象及數(shù)組。Mongo的查詢優(yōu)化 器會(huì)分析查詢表達(dá)式,并生成一個(gè)高效的查詢計(jì)劃。 查 詢監(jiān)視:Mongo包含一個(gè)監(jiān)視工具 用于分析數(shù)據(jù)庫操作的性能。 復(fù)制 及自動(dòng)故障轉(zhuǎn)移:Mongo數(shù)據(jù)庫支持服務(wù)器 之間的數(shù)據(jù)復(fù)制,支持主-從模式及服務(wù) 器之間的相互復(fù)制。復(fù)制的主要目標(biāo)是提供冗余及自
5、 動(dòng)故障轉(zhuǎn)移。 高效的傳統(tǒng)存儲(chǔ)方式:支持二進(jìn)制數(shù)據(jù)及大型對(duì)象(如照片或圖片)。 自動(dòng)分片以支持云級(jí)別的伸縮性(處于 早期alpha階段):自動(dòng)分片功能支持水平的數(shù)據(jù)庫集群 ,可動(dòng)態(tài)添加額外的機(jī)器。 MongoDB的主要目標(biāo)是在鍵/值存儲(chǔ)方式(提供了高性能和高度伸縮性)以及傳統(tǒng)的RDBMS系統(tǒng) (豐富的功能)架起一座橋梁,集兩者的優(yōu)勢(shì)于一 身。根據(jù)官方網(wǎng)站的描述,Mongo 適合用于以下場(chǎng)景:網(wǎng)站數(shù)據(jù):Mongo非常適合實(shí)時(shí)的插入,更新與查詢,并具備網(wǎng)站實(shí)時(shí)數(shù)據(jù)存儲(chǔ)所需的復(fù)制及高度伸縮性。 緩存 :由于性能很高,Mongo也適合作為信息基礎(chǔ)設(shè)施的緩存層。在系統(tǒng)重啟之后,由Mongo搭建的持久化緩
6、存層可以避免下層的數(shù)據(jù)源 過載。 大尺寸,低價(jià)值的數(shù)據(jù):使用傳統(tǒng)的關(guān)系型數(shù)據(jù)庫存儲(chǔ)一些數(shù)據(jù)時(shí)可能會(huì)比較昂貴,在此之前,很 多時(shí)候程序員往往會(huì)選擇傳統(tǒng)的文件 進(jìn)行存儲(chǔ)。 高伸縮性的場(chǎng)景:Mongo非常適合由數(shù)十或數(shù)百臺(tái)服務(wù)器組成的數(shù)據(jù)庫。Mongo的路線圖中已經(jīng)包含對(duì)MapReduce引擎的內(nèi)置支 持。 用于 對(duì)象及JSON數(shù)據(jù)的存儲(chǔ):Mongo的BSON數(shù)據(jù)格式非常適合文檔化格式的存儲(chǔ) 及查詢。 自然,MongoDB的使用也會(huì)有一些限制,例如它不適合:高度事務(wù)性的系統(tǒng):例如銀行或會(huì)計(jì)系統(tǒng)。傳統(tǒng)的關(guān)系型數(shù)據(jù)庫目前還是更適用于需要大量原子性復(fù)雜事務(wù)的應(yīng)用 程序。 傳統(tǒng)的商業(yè)智能應(yīng)用:針對(duì)特定問題的
7、BI數(shù)據(jù)庫會(huì)對(duì)產(chǎn)生高度優(yōu)化的查詢方式。對(duì)于此類應(yīng)用,數(shù)據(jù)倉(cāng)庫可能是更合適的選擇。 需要SQL的問題 MongoDB支持OS X、Linux及Windows等操作系統(tǒng) ,并提供了Python,PHP,Ruby,Java,C,C#,Javascript,Perl及C+語言的驅(qū)動(dòng)程序,社區(qū)中也提供了對(duì)Erlang 及.NET等平臺(tái)的驅(qū)動(dòng)程序MongoDB下載: HYPERLINK /display/DOCS/Downloads /display/DOCS/Downloads HYPERLINK /lin /lin . 686-v1.2-latest.tgz MongoDB安裝:falconwww.f
8、wphp .cn /mongodb$tar xvzf mongodb-linux -i686-v1.2-latest.tgz falcon /mongodb$mv mongodb-linux-i686-v1.2-latest mongodb falcon /mongodb$cd mongodb falcon /mongodb$ ls mongo mongodump mongofiles mongorestore mongosniff mongod mongoexport mongoimport mongos 只需要解壓到相關(guān)目錄即可,不需要編譯安裝MongoDB啟動(dòng)與關(guān)閉:mongoDB的服務(wù)
9、器端程序?yàn)閙ongodfalcon /mongodb$ bin/mongod -help * NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data * see HYPERLINK /post/137788967/32-bit-limitations /post/137788967/32-bit-limitations for more Allowed options: General options: -h -help show this usage information -version
10、show version information -f -config arg configuration file specifying additional options -port arg specify port number -bind _ip arg local ip address to bind listener - all local ips bound by default -v -verbose be more verbose (include multiple times for more verbosity e.g. -vvvvv) -dbpath arg (=/d
11、ata/db/) directory for datafiles 指定數(shù)據(jù)存放目錄 -quiet quieter output 靜默模式 -logpath arg file to send all output to instead of stdout 指定日志 存放目錄 -logappend appnd to logpath instead of over-writing 指定日志是以追加還是以覆蓋的方式寫入日志文件 -fork fork server process 以創(chuàng)建子進(jìn)程 的方式運(yùn)行 -cpu periodically show cpu and iowait utilization
12、 周期性的顯示cpu和io的使用情況 -noauth run without security 無認(rèn)證模式運(yùn)行 -auth run with security 認(rèn)證模式運(yùn)行 -objcheck inspect client data for validity on receipt 檢查客戶端輸入數(shù)據(jù)的有效性檢查 -quota enable db quota management 開始數(shù)據(jù)庫配額的管理 -quotaFiles arg number of files allower per db, requires -quota 規(guī)定每個(gè)數(shù)據(jù)庫允許的文件數(shù) -appsrvpath arg root
13、 directory for the babble app server -nocursors diagnostic/debugging option 調(diào)試診斷選項(xiàng) -nohints ignore query hints 忽略查詢命中率 -nohttpinterface disable http interface 關(guān)閉http接口,默認(rèn)是28017 -noscripting disable scripting engine 關(guān)閉腳本 引擎 -noprealloc disable data file preallocation 關(guān)閉數(shù)據(jù)庫文件大小預(yù)分配 -smallfiles use a sm
14、aller default file size 使用較小的默認(rèn)文件大小 -nssize arg (=16) .ns file size (in MB) for new databases 新數(shù)據(jù)庫ns文件的默認(rèn)大小 -diaglog arg 0=off 1=W 2=R 3=both 7=W+some reads 提供的方式,是只讀,只寫,還是讀寫都行,還是主要寫+部分的讀模式 -sysinfo print some diagnostic system information 打印系統(tǒng)診斷信息 -upgrade upgrade db if needed 如果需要就更新數(shù)據(jù)庫 -repair ru
15、n repair on all dbs 修復(fù)所有的數(shù)據(jù)庫 -notablescan do not allow table scans 不運(yùn)行表掃描 -syncdelay arg (=60) seconds between disk syncs (0 for never) 系統(tǒng)同步刷新磁盤 的時(shí)間,默認(rèn)是60s Replication options: -master master mod e 主復(fù)制模式 -slave slave mode 從復(fù)制模式 -source arg when slave: specify master as 當(dāng)為從時(shí),指定主的地址和端口 -only arg when
16、slave: specify a single database to replicate 當(dāng)為從時(shí),指定需要從主復(fù)制的單一庫 -pairwith arg address of server to pair with -arbiter arg address of arbiter server 仲裁服務(wù)器,在主主中和pair中用到 -autoresync automatically resync if slave data is stale 自動(dòng)同步從的數(shù)據(jù) -oplogSize arg size limit (in MB) for op log 指定操作日志的大小 -opIdMem arg
17、size limit (in bytes) for in memory storage of op ids指定存儲(chǔ)操作日志的內(nèi)存 大小 Sharding options: -configsvr declare this is a config db of a cluster 指定shard中的配置服務(wù)器 -shardsvr declare this is a shard db of a cluster 指定shard服務(wù)器 在 啟動(dòng)mongoDB之前,我們必須新建一個(gè)存放mongoDB數(shù)據(jù)和日志的目錄falcon $ mkdir m_data m_log 到此mongodb啟動(dòng)的準(zhǔn)備工作做完了
18、,現(xiàn)在我們 啟動(dòng)mongodbfalcon /mongodb$ bin/mongod -dbpath=/home/falcon/m_data -logpath=/home/falcon/m_log -logappend & 現(xiàn)在mongodb啟動(dòng)了,檢查是否啟動(dòng)的方法:falcon /mongodb$ ps -ef|grep mongod falcon 2533 2271 0 08:21 pts/0 00:00:00 bin/mongod -dbpath=/home/falcon/m_data -logpath=/home/falcon/m_log -logappend falcon 2541
19、 2271 0 08:22 pts/0 00:00:00 grep mongod 查看mongodb的端口是否啟動(dòng),默認(rèn)是 28017,在啟動(dòng)服務(wù)器時(shí),可以通過-port來指定falcon /mongodb$ netstat -an -t|grep 28017 tcp 0 0 :28017 :* LISTEN 到此,證明mongoDB已經(jīng)啟動(dòng)完成關(guān)閉的方法很簡(jiǎn)單:Killall mongod 或者是 kill pid使用方法: 利用客戶端程序mongo登錄mongoDBfalcon /mongodb$ bin/mongo MongoDB shell version: 1.2.4- url: t
20、est connecting to: test type help for help help HELP Show dbs 顯示數(shù)據(jù)庫名 show collections 顯示當(dāng)前數(shù)據(jù)庫中的集合集 show users 顯示當(dāng)前數(shù)據(jù)庫的用戶 show profile 顯示最后系統(tǒng)用時(shí)大于1ms的系統(tǒng)概要 use 切換到數(shù)據(jù)庫 db.help() help on DB methods db.foo.help() help on collection methods db.foo.find() list objects in collection foo db.foo.find( a : 1 )
21、list objects in foo where a = 1 it result of the last line evaluated; use to further iterate show dbs 默認(rèn)情況下有2數(shù)據(jù)庫 admin local use admin 切換到admin數(shù)據(jù)庫 switched to db admin show collections 顯示admin數(shù)據(jù)庫下面的集合集 system.indexes 下面我們來簡(jiǎn)單的新建集合集,插入、更新、查詢數(shù)據(jù), 體驗(yàn)mongodb帶給我們不一樣的生活新建數(shù)據(jù)庫的方法是在新建集合集: db.createCollection(u
22、ser); ok : 1 show collections system.indexes user 插入數(shù)據(jù): db.user.insert(uid:1,username:Falcon.C,age:25); db.user.insert(uid:2,username:aabc,age:24); 查詢數(shù)據(jù): db.user.find(); _id : ObjectId(4b81e74c1f0fd3b9545cba43), uid : 1, username : Falcon.C, age : 25 _id : ObjectId(4b81e74d1f0fd3b9545cba44), uid : 2
23、, username : aabc, age : 24 查詢數(shù)據(jù)的方式很豐富,有類似于SQL的條件查詢,將 會(huì)在以后的文檔中詳細(xì)介紹如:我想查詢uid為1的用戶信息 db.user.find(uid:1); _id : ObjectId(4b81e74c1f0fd3b9545cba43), uid : 1, username : Falcon.C, age : 25 等等,豐富的查詢還有l(wèi)imit ,sort ,findOne,distinct等更新數(shù)據(jù): db.user.update(uid:1,$set:age:26) db.user.find(); _id : ObjectId(4b81
24、e76f1f0fd3b9545cba45), uid : 1, username : Falcon.C, age : 26 _id : ObjectId(4b81e7701f0fd3b9545cba46), uid : 2, username : aabc, age : 24 db.user.update(uid:1,$inc:age:-1) db.user.find(); _id : ObjectId(4b81e76f1f0fd3b9545cba45), uid : 1, username : Falcon.C, age : 25 _id : ObjectId(4b81e7701f0fd3b
25、9545cba46), uid : 2, username : aabc, age : 24 出 了以上的2種用法,更新的條件還有$unset、$push 、$pushAll 、$pop 、$pull 、$pullAll以上就是MongoDB簡(jiǎn)單的使用介紹,在以后的文檔中將會(huì)詳細(xì)的介紹mongoDB非常酷的CURD方法,mongoDB的Replication及分 布式開發(fā)文檔: HYPERLINK /display/DOCS/Developer+Zone /display/DOCS/Developer+Zone 管理文檔: HYPERLINK /display/DOCS/Admin+Zone
26、/display/DOCS/Admin+Zone 下載地址: HYPERLINK /display/DOCS/Downloads /display/DOCS/DownloadsMongoDB主從復(fù)制介紹MongoDB的主從復(fù)制其實(shí)很簡(jiǎn)單,就是在運(yùn)行 主的服務(wù)器 上開啟mongod進(jìn)程 時(shí),加入?yún)?shù)-master即可,在運(yùn)行從的服務(wù) 器上開啟mongod進(jìn)程時(shí),加入-slave 和 -source 指定主即可,這樣,在主數(shù)據(jù) 庫更新時(shí),數(shù)據(jù)被復(fù)制到從數(shù)據(jù)庫 中(這里日志文件和訪問數(shù)據(jù)時(shí)授權(quán)用戶暫時(shí)不考慮 )下面我在單臺(tái)服務(wù)器上開啟2deamon來模擬2臺(tái)服務(wù)器進(jìn)行主從復(fù)制:$ mkdir m_m
27、aster m_slave $mongodb/bin/mongod -port 28018 -dbpath /m_master -master & $mongodb/bin/mongod -port 28019 -dbpath /m_slave -slave -source localhost:28018 & 這樣主從服務(wù)器都已經(jīng)啟動(dòng)了,可以利用 netstat -an -t 查看28018、28019端口 是否開放登錄主服務(wù)器:$ mongodb/bin/mongo -port 28018 MongoDB shell version: 1.2.4- url: test connecting
28、to: :28018/test type help for help show dbs admin local test use test switched to db test show collections 這里主上的test數(shù)據(jù)什么表都沒有,為空,查看從服 務(wù)器同樣也是這樣$ mongodb/bin/mongo -port 28019 MongoDB shell version: 1.2.4- url: test connecting to: :28019/test type help for help show dbs admin local test use test switch
29、ed to db test show collections 那么現(xiàn)在我們來驗(yàn)證主從數(shù)據(jù)是否會(huì)像想象的那樣同步 呢?我們?cè)谥魃闲陆ū韚ser db test db.createCollection(user); show collections system.indexes user 表 user已經(jīng)存在了,而且test庫中還多了一個(gè)system.indexes用來存放索引的表到從服務(wù)器上查看test庫: db test show collections system.indexes User db.user.find(); 從服務(wù)器的test庫中user表已經(jīng)存在,同時(shí)我還查了一下user表
30、為空現(xiàn)在我們?cè)賮頊y(cè)試一下,向主服務(wù)器test庫的user表中插入一條數(shù)據(jù) show collections system.indexes user db.user.insert(uid:1,name:Falcon.C,age:25); db.user.find(); _id : ObjectId(4b8226a997521a578b7aea38), uid : 1, name : Falcon.C, age : 25 這 時(shí)我們查看從服務(wù)器的test庫user表時(shí)會(huì)多出一條記錄來: db.user.find(); _id : ObjectId(4b8226a997521a578b7aea38)
31、, uid : 1, name : Falcon.C, age : 25 MongoDB 還有 Replica Pairs 和 Master - Master參考地址: HYPERLINK /display/DOCS/Master+Slave /display/DOCS/Master+SlaveMongoDB一般情況下都可以支持主主復(fù)制,但是在大部分情況下官方不推薦使用運(yùn)行 的master - master的準(zhǔn)備工作是:新建存放數(shù)據(jù) 庫文件 的路徑$mkdir mongodata/mm_28050 mongodata/mm_28051 運(yùn)行mongodb數(shù)據(jù)庫 ,一個(gè)端口 為:28050,一個(gè)
32、為:28051$ mongodb/bin/mongod -port 28050 -dbpath /mongodata/mm_28050 -master -slave -source localhost:28051 /dev/null & $ mongodb/bin/mongod -port 28051 -dbpath mongodata/mm_28051 -master -slave -source localhost:28050 /dev/null & 可以通過ps -ef|grep mongod 或 netstat -an -t來檢查是否運(yùn)行功能測(cè)試master - master模式 :$
33、 mongodb/bin/mongo -port 28050 MongoDB shell version: 1.2.4- url: test connecting to: :28050/test type help for help show dbs admin local db test db.user.insert(_id:1,username:Falcon.C,age:25,sex:M); db.user.find(); _id : 1, username : Falcon.C, age : 25, sex : M db.user.find(); /在28051端口插入數(shù)據(jù)后,再來查詢,
34、看數(shù)據(jù)是否同步 _id : 1, username : Falcon.C, age : 25, sex : M _id : 2, username : NetOne, age : 24, sex : F $ mongodb/bin/mongo -port 28051 MongoDB shell version: 1.2.4- url: test connecting to: :28051/test type help for help db test show collections 端口28050已經(jīng)新建了一個(gè)user表并插入了一條數(shù)據(jù),這里多出2表 system.indexes user
35、db.user.find(); /查詢表user發(fā)現(xiàn)數(shù)據(jù)已經(jīng)同步 _id : 1, username : Falcon.C, age : 25, sex : M db.user.insert(_id:2,username:NetOne,age:24,sex:F);在此插入數(shù)據(jù)看數(shù)據(jù)是否雙向同步 db.user.find(); _id : 1, username : Falcon.C, age : 25, sex : M _id : 2, username : NetOne, age : 24, sex : F 通 過以上開啟兩終端分別連接到28050、28051端口,分別插入測(cè)試數(shù)據(jù)發(fā)現(xiàn),一切
36、正常,正如我們所想的那樣實(shí)現(xiàn)數(shù)據(jù)的雙向同步參考信息: HYPERLINK /display/DOCS/Master+Master+Replication /display/DOCS/Master+Master+ReplicationMongoDB的使用技巧如果想查看當(dāng)前連接在哪個(gè)數(shù)據(jù) 庫下面,可以直接輸入db db Admin 想切換到test數(shù)據(jù)庫 use test switched to db test db Test 想 查看test下有哪些表或者叫collection,可以輸入 show collections system.indexes user 想 知道m(xù)ongodb支持哪些命令
37、 ,可以直接輸入help help HELP show dbs show database names show collections show collections in current database show users show users in current database show profile show most recent file entries with time = 1ms use set curent database to db.help() help on DB methods db.foo.help() help on collection meth
38、ods db.foo.find() list objects in collection foo db.foo.find( a : 1 ) list objects in foo where a = 1 it result of the last line evaluated; use to further iterate 如果想知道當(dāng)前數(shù)據(jù)庫支持哪些方法: db.help(); DB methods: db.addUser(username, password) 添加數(shù)據(jù)庫授權(quán)用戶 db.auth(username, password) 訪問 認(rèn)證 db.cloneDatabase(from
39、host) 克隆數(shù)據(jù)庫 mandHelp(name) returns the help for the command db.copyDatabase(fromdb, todb, fromhost) 復(fù)制數(shù)據(jù)庫 db.createCollection(name, size : ., capped : ., max : . ) 創(chuàng)建表 db.currentOp() displays the current operation in the db db.dropDatabase() 刪除當(dāng)前數(shù)據(jù)庫 db.eval(func, args) run code server-side db.getCol
40、lection(cname) same as dbcname or ame db.getCollectionNames() 獲取當(dāng)前數(shù)據(jù)庫的表名 db.getLastError() - just returns the err msg string db.getLastErrorObj() - return full status object db.getMongo() get the server connection object db.getMongo().setSlaveOk() allow this connection to read from the nonmaster mem
41、ber of a replica pair db.getName() db.getPrevError() db.getProfilingLevel() db.getReplicationInfo() db.getSisterDB(name) get the db at the same server as this onew db.killOp() kills the current operation in the db db.printCollectionStats() 打印各表的狀態(tài)信息 db.printReplicationInfo() 打印主數(shù)據(jù)庫的復(fù)制狀態(tài)信息 db.printSl
42、aveReplicationInfo() 打印從數(shù)據(jù)庫的復(fù)制狀態(tài)信息 db.printShardingStatus() 打印分片狀態(tài)信息 db.removeUser(username) 刪除數(shù)據(jù)庫用戶 db.repairDatabase() 修復(fù)數(shù)據(jù)庫 db.resetError() db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into cmdObj : 1 db.setProfilingLevel(level) 0=off 1=slow 2=all db.shutdownServe
43、r () db.version() current version of the server 如果想知道當(dāng)前數(shù)據(jù)庫下的表或者表 collection支持哪些方法,可以使用一下命令如: db.user.help(); user為表名 DBCollection help db.foo.count() 統(tǒng)計(jì)表的行數(shù) db.foo.dataSize() 統(tǒng)計(jì)表數(shù)據(jù)的大小 db.foo.distinct( key ) - eg. db.foo.distinct( x ) 按照給定的條件除重 db.foo.drop() drop the collection 刪除表 db.foo.dropIndex(n
44、ame) 刪除指定索引 db.foo.dropIndexes() 刪除所有索引 db.foo.ensureIndex(keypattern,options) - options should be an object with these possible fields: name, unique, dropDups 增加索引 db.foo.find( query , fields) - first parameter is an optional query filter. second parameter is optional set of fields to return. 根據(jù)條件查
45、找數(shù)據(jù) e.g. db.foo.find( x : 77 , name : 1 , x : 1 ) db.foo.find(.).count() db.foo.find(.).limit(n) 根據(jù)條件查找數(shù)據(jù)并返回指定記錄數(shù) db.foo.find(.).skip(n) db.foo.find(.).sort(.) 查找排序 db.foo.findOne(query) 根據(jù)條件查詢只查詢一條數(shù)據(jù) db.foo.getDB() get DB object associated with collection 返回表所屬的庫 db.foo.getIndexes() 顯示表的所有索引 db.foo
46、.group( key : ., initial: ., reduce : ., cond: . ) 根據(jù)條件分組 db.foo.mapReduce( mapFunction , reduceFunction , ) db.foo.remove(query) 根據(jù)條件刪除數(shù)據(jù) db.foo.renameCollection( newName ) renames the collection 重命名表 db.foo.save(obj) 保存數(shù)據(jù) db.foo.stats() 查看表的狀態(tài) db.foo.storageSize() - includes free space allocated t
47、o this collection 查詢分配到表空間大小 db.foo.totalIndexSize() - size in bytes of all the indexes 查詢所有索引的大小 db.foo.totalSize() - storage allocated for all data and indexes 查詢表的總大小 db.foo.update(query, object, upsert_bool) 根據(jù)條件更新數(shù)據(jù) db.foo.validate() - SLOW 驗(yàn)證表的詳細(xì)信息 db.foo.getShardVersion() - only for use with
48、sharding Mongodb的備份工具 mongodump如果想備份數(shù)據(jù)庫test 如:falconwww.fwphp .cn /mongodb/bin$ ./mongodump -help options: -help produce help message -h -host arg mongo host to connect to -d -db arg database to use -c -collection arg collection to use (some commands) -u -username arg username -p -password arg passw
49、ord -dbpath arg directly access mongod data files in this path, instead of connecting to a mongod instance -v -verbose be more verbose (include multiple times for more verbosity e.g. -vvvvv) -o -out arg (=dump) output directory falcon /mongodb/bin$ color=Blue./mongodump -d test -o test/color connect
50、ed to: DATABASE: test to test/test test.user to test/test/user.bson 100000 objects test.system.indexes to test/test/system.indexes.bson 1 objects falcon /mongodb/bin$ ls 2 mongo mongodump mongofiles mongorestore mongosniff dump mongod mongoexport mongoimport mongos test MongoDB的數(shù)據(jù)恢復(fù)工具 mongorestore查看
51、test庫中的表 show collections system.indexes User 刪除user表 db.user.drop(); True show collections System.indexes 現(xiàn)在利用mongorestore表恢復(fù)剛才利用 mongodump備份的數(shù)據(jù)falcon /mongodb/bin$ ./mongorestore -help usage: ./mongorestore options directory or filename to restore from options: -help produce help message -h -host
52、arg mongo host to connect to -d -db arg database to use -c -collection arg collection to use (some commands) -u -username arg username -p -password arg password -dbpath arg directly access mongod data files in this path, instead of connecting to a mongod instance -v -verbose be more verbose (include
53、 multiple times for more verbosity e.g. -vvvvv) falcon /mongodb/bin$ ./mongorestore -d test -c user test/test/user.bson connected to: test/test/user.bson going into namespace test.user 100000 objects User表中的10w條記錄已經(jīng)恢復(fù) show collections system.indexes user db.user.find(); _id : ObjectId(4b9c8db08ead0e
54、3347000000), uid : 1, username : Falcon.C-1 _id : ObjectId(4b9c8db08ead0e3347010000), uid : 2, username : Falcon.C-2 _id : ObjectId(4b9c8db08ead0e3347020000), uid : 3, username : Falcon.C-3 _id : ObjectId(4b9c8db08ead0e3347030000), uid : 4, username : Falcon.C-4 _id : ObjectId(4b9c8db08ead0e33470400
55、00), uid : 5, username : Falcon.C-5 . has more mongodb還提供了HTTP查看運(yùn)行 狀態(tài)及restfull的接口默認(rèn)的訪問端口 是28017rest的訪問接口參考地址: HYPERLINK /display/DOCS/Http+Interface /display/DOCS/Http+InterfaceMongoDB的sharding功能MongoDB的auto-sharding功能是指mongodb通過mongos自動(dòng)建立一個(gè)水平擴(kuò)展的數(shù)據(jù) 庫集群 系統(tǒng) ,將數(shù)據(jù)庫 分表存儲(chǔ)在sharding的各個(gè)節(jié)點(diǎn)上。一個(gè)mongodb集群包括一些sha
56、rds(包括一些mongod進(jìn)程 ),mongos路由進(jìn)程,一個(gè)或多個(gè)config服務(wù)器 Shards每一個(gè)shard包括一個(gè)或多個(gè)服務(wù) 和存儲(chǔ)數(shù)據(jù)的mongod進(jìn)程(mongod是 MongoDB數(shù)據(jù)的核心進(jìn)程)典型的每個(gè)shard開啟多個(gè)服務(wù)來提高服務(wù)的可用性。這些服務(wù)/mongod進(jìn)程在shard中組成一個(gè)復(fù)制集ChunksChunk是一個(gè)來自特殊集合中的一個(gè)數(shù)據(jù)范圍,(collection,minKey,maxKey)描敘一個(gè)chunk,它介于minKey和 maxKey范圍之間。例如chunks 的maxsize大小是100M,如果一個(gè)文件 達(dá)到或超過這個(gè)范圍時(shí),會(huì)被切分到2個(gè)新的
57、chunks中。當(dāng)一個(gè)shard的數(shù)據(jù)過量時(shí),chunks將會(huì)被遷移到其他的shards上。同樣,chunks也可以遷移到其他的shards上Config ServersConfig服務(wù)器存儲(chǔ)著集群的metadata信息,包括每個(gè)服務(wù)器,每個(gè)shard的基本信息和chunk信息Config服務(wù)器主要存儲(chǔ)的是chunk信息。每一個(gè)config服務(wù)器都復(fù)制了完整的chunk信息。配置:(模擬2個(gè)shard服務(wù)和一個(gè)config服務(wù))Shard1:27020Shard2:27021Config:27022Mongos啟動(dòng)時(shí)默認(rèn)使用的27017端口 新建存放數(shù)據(jù)的目錄falconwww.fwphp .
58、cn /mongodata$ mkdir 27020 27021 27022 falcon /mongodata$ ls 27020 27021 27022 falcon /mongodb/bin$ ./mongod -dbpath /home/falcon/mongodata/27020 -port 27020 /home/falcon/mongodata/27020.log & falcon /mongodb/bin$ ./mongod -dbpath /home/falcon/mongodata/27021 -port 27021 /home/falcon/mongodata/27021
59、.log & falcon /mongodb/bin$ ./mongod -dbpath /home/falcon/mongodata/27022 -port 27022 /home/falcon/mongodata/27022.log & 啟動(dòng)mongos時(shí),默認(rèn)開啟了27017端口falcon /mongodb/bin$ ./mongos -configdb localhost:27022 /home/falcon/mongodata/config.log & 檢查是否啟動(dòng)falcon /mongodb/bin$ ps -ef|grep mongo falcon 2612 1 0 20:1
60、5 ? 00:00:00 ./mongod -dbpath /home/falcon/mongodata/27020 -port 27020 falcon 2619 1 0 20:15 ? 00:00:00 ./mongod -dbpath /home/falcon/mongodata/27021 -port 27021 falcon 2625 1 0 20:15 ? 00:00:00 ./mongod -dbpath /home/falcon/mongodata/27022 -port 27022 falcon 2658 1 0 20:15 ? 00:00:00 ./mongos -conf
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 開工禮品活動(dòng)策劃方案(3篇)
- 大型的活動(dòng)方案策劃(3篇)
- 醫(yī)療防疫志愿者流行病學(xué)調(diào)查
- 醫(yī)療質(zhì)量持續(xù)改進(jìn)與DRG支付
- 感染科醫(yī)院感染控制規(guī)范培訓(xùn)方案
- 醫(yī)療設(shè)備采購(gòu)與運(yùn)營(yíng)效率優(yōu)化
- 護(hù)理帶教工作社會(huì)影響匯報(bào)
- 社區(qū)護(hù)理:健康促進(jìn)活動(dòng)設(shè)計(jì)
- 2026年惠州市公安局招聘警務(wù)輔助人員612人備考題庫及一套參考答案詳解
- 2026年·包頭稀土高新區(qū)教育系統(tǒng)校園招聘25人備考題庫(三)(北京師范大學(xué)招聘站)有答案詳解
- 2025年上半年遼寧大連市總工會(huì)面向社會(huì)招聘社會(huì)化工會(huì)工作者42人重點(diǎn)基礎(chǔ)提升(共500題)附帶答案詳解
- DBJ50-T-137-2023建筑邊坡工程現(xiàn)場(chǎng)檢測(cè)技術(shù)標(biāo)準(zhǔn)
- 第三方代付款協(xié)議書
- 房地產(chǎn)售后服務(wù)及質(zhì)量保證措施
- 感應(yīng)加熱器安全操作規(guī)程
- 商業(yè)地產(chǎn)行業(yè)商業(yè)地產(chǎn)投資機(jī)會(huì)
- 兩輪車控制器行業(yè)報(bào)告
- JSA臨時(shí)用電作業(yè)安全分析表
- 2015-2022年北京衛(wèi)生職業(yè)學(xué)院高職單招語文/數(shù)學(xué)/英語筆試參考題庫含答案解析
- 賽膚潤(rùn)常見臨床應(yīng)用2010年
- 提高鋁模板施工質(zhì)量合格率
評(píng)論
0/150
提交評(píng)論