初步使用總結(jié)_第1頁(yè)
初步使用總結(jié)_第2頁(yè)
初步使用總結(jié)_第3頁(yè)
初步使用總結(jié)_第4頁(yè)
初步使用總結(jié)_第5頁(yè)
已閱讀5頁(yè),還剩5頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、mongoDB2.6使用總結(jié) 一、準(zhǔn)備工作下載java驅(qū)動(dòng)包驅(qū)動(dòng)包下載地址:/artifact/org.mongodb/mongo-java-drivermongoDB下載:/在線api:/manual/applications/drivers/二、安裝Mongo1.windows下安裝方式:安裝Mongo數(shù)據(jù)庫(kù):第一步:下載安裝包:如果是win系統(tǒng),注意是64位還是32位版本的,請(qǐng)選擇正確的版本(偶數(shù)為發(fā)布版、奇數(shù)為開(kāi)發(fā)版)。第二步:新建目錄“F:mongodb

2、2”,解壓下載到的安裝包第三步:在“F:mongodb2”目錄下新建“db”文件夾,它將會(huì)作為數(shù)據(jù)存放的根文件夾。配置Mongo服務(wù)端:打開(kāi)CMD窗口,按照如下方式輸入命令: d: cd F:mongodb2bin mongod -dbpath ./db啟動(dòng)成功后從瀏覽器訪問(wèn):http:/localhost:27017/標(biāo)明windows下的mongodb已經(jīng)啟動(dòng)成功;數(shù)據(jù)庫(kù)啟動(dòng)完成,接下來(lái)是創(chuàng)建數(shù)據(jù)庫(kù)和集合:mongo show dbsadmin (empty)local 0.078GB use admin(切換管理用戶)switched to db admin db.my_mongo(創(chuàng)建

3、數(shù)據(jù)庫(kù))admin.my_mongo db.addUser(abc,abc123)(添加用戶)WARNING: The addUser shell helper is DEPRECATED. Please use createUser insteadSuccessfully added user: user : abc, roles : root db.auth(abc,abc123)(添加登陸用戶)1 db.createCollection(t_users)(添加表) ok : 1 show collections(顯示表)system.indexessystem.userssystem.v

4、ersiont_users db.t_users.save(age:21)(存儲(chǔ)數(shù)據(jù))WriteResult( nInserted : 1 ) db.t_users.find()(查詢所有數(shù)據(jù)) _id : ObjectId(53a2e45e4ab4ac5398), age : 21 mongodb 刪除數(shù)據(jù)庫(kù)use mymongo;db.dropDatabase();mongodb刪除表db.t_users.drop();三、Java操作MongoDB示例1、 建立Test.java,完成簡(jiǎn)單的mongoDB數(shù)據(jù)庫(kù)操作package com;import .UnknownHo

5、stException;import java.util.Date;import com.mongodb.BasicDBObject;import com.mongodb.DB;import com.mongodb.DBCollection;import com.mongodb.DBCursor;import com.mongodb.DBObject;import com.mongodb.Mongo;import com.mongodb.MongoException;public class Test static DBCollection coll = null; static DB db

6、= null; static Mongo m = null; try m = new Mongo(localhost, 27017); catch (UnknownHostException e) e.printStackTrace(); catch (MongoException e) e.printStackTrace(); db = m.getDB(my_mongo); if (db.authenticate(prx, prx.toCharArray() System.out.println(auth success); public static void main(String ar

7、gs) throws Exception / getMemo(); / insertMemo(); query(); / delete(); /* * * param collectionName 相當(dāng)于 Table 名 * return * throws Exception */ public static DBCollection getDBCollection(String collectionName) throws Exception if(coll = null) coll = db.getCollection(collectionName); return coll; publi

8、c static void insertMemo() throws Exception DBCollection coll = getDBCollection(member); BasicDBObject doc = new BasicDBObject(); doc.put(name, prx); doc.put(city, changsha); doc.put(time, new Date(); coll.insert(doc); public static void getMemo() throws Exception DBCollection coll = getDBCollection

9、(member); BasicDBObject obj = (BasicDBObject) coll.findOne(); System.out.println(obj); public static void query() throws Exception DBCollection coll = getDBCollection(member); BasicDBObject obj = new BasicDBObject(); obj.put(name, prx); DBCursor cursor = coll.find(obj); while (cursor.hasNext() Date

10、date = (Date) cursor.next().get(time); System.out.println(date); cursor.close(); public static void delete() throws Exception DBCollection coll = getDBCollection(member); BasicDBObject query = new BasicDBObject(); query.put(name, prx); / 找到并且刪除,并返回刪除的對(duì)象 DBObject removeObj = coll.findAndRemove(query)

11、; System.out.println(removeObj); 四、mongoDb語(yǔ)法與mysql語(yǔ)法比較MongoDB語(yǔ)法 MySql語(yǔ)法db.test.find(name:foobar) select * from test where name=foobardb.test.find() select * from testdb.test.find(ID:10).count() select count(*) from test where ID=10db.test.find().skip(10).limit(20) select * from test limit 10,20db.te

12、st.find(ID:$in:25,35,45) select * from test where ID in (25,35,45)db.test.find().sort(ID:-1) select * from test order by ID descdb.test.distinct(name,ID:$lt:20) select distinct(name) from test where ID20db.test.group(key:name:true,cond:name:foo,reduce:function(obj,prev)prev.msum+=obj.marks;,initial:

13、msum:0) select name,sum(marks) from test group by namedb.test.find(this.ID20,name:1) select name from test where ID20db.test.insert(name:foobar,age:25)insert into test (name,age) values(foobar,25)db.test.remove() delete * from testdb.test.remove(age:20) delete test where age=20db.test.remove(age:$lt

14、:20) elete test where age20db.test.remove(age:$lte:20) delete test where age=20db.test.remove(age:$gt:20) delete test where age20db.test.remove(age:$gte:20) delete test where age=20db.test.remove(age:$ne:20) delete test where age!=20db.test.update(name:foobar,$set:age:36) update test set age=36 wher

15、e name=foobardb.test.update(name:foobar,$inc:age:3) update test set age=age+3 where name=foobar五、主從復(fù)制/huangxincheng/archive/2012/03/04/.html主服務(wù)器上的數(shù)據(jù)能同步到從服務(wù)器。六、副本集/huangxincheng/archive/2012/03/04/.html1)-清空所有db文件夾2)-primarymongod -dbpath=D:mongodb2db -port 2

16、222 -replSet shopex/:33333)-secondarymongod -dbpath=E:mongodb2db -port 3333 -replSet shopex/:22224)-初始化db.runCommand(replSetInitiate:_id:shopex,members:_id:1,host::2222,_id:2,host::3333)5)-仲裁mongod -dbpath=F:mongodb2db -port 4444 -replSet shopex/:22226)-客戶端2222/adminmongo :2222/adminrs.addArb(:4444)rs.status()7)-故障自動(dòng)回復(fù)KO掉2222稍等一會(huì)兒,3333自動(dòng)變成primary8)-客戶端3333/adminrs.status()9)-恢復(fù)啟動(dòng)2222,2222變成secondary,3333是primary,4444仍是仲裁。七、分片技術(shù)/huangxincheng/archive/2012/03/07/.htmlmongodb采用將集合進(jìn)行拆分,然后將拆分的數(shù)據(jù)均攤到幾個(gè)片上的一種解決方案。下面我

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論