天天看點

MongoDB基本介紹及一些用法

 面向文檔的非關系資料庫主要解決的問題不是高性能的并發讀寫,而是保證海量資料存儲的同時,具有良好的查詢性能。MongoDB是用C++開發的,而CouchDB則是Erlang開發的:

  MongoDB

  MongoDB是一個介于關系資料庫和非關系資料庫之間的産品,是非關系資料庫當中功能最豐富,最像關系資料庫的。他支援的資料結構非常松散,是類似 json的bjson格式,是以可以存儲比較複雜的資料類型。Mongo最大的特點是他支援的查詢語言非常強大,其文法有點類似于面向對象的查詢語言,幾 乎可以實作類似關系資料庫單表查詢的絕大部分功能,而且還支援對資料建立索引。

  Mongo主要解決的是海量資料的通路效率問題,根據官方的文檔,當資料量達到50GB以上的時候,Mongo的資料庫通路速度是MySQL的 10倍以上。Mongo的并發讀寫效率不是特别出色,根據官方提供的性能測試表明,大約每秒可以處理0.5萬-1.5次讀寫請求。對于Mongo的并發讀 寫性能,我(robbin)也打算有空的時候好好測試一下。

  因為Mongo主要是支援海量資料存儲的,是以Mongo還自帶了一個出色的分布式檔案系統GridFS,可以支援海量的資料存儲,但我也看到有些評論認為GridFS性能不佳,這一點還是有待親自做點測試來驗證了。

  最後由于Mongo可以支援複雜的資料結構,而且帶有強大的資料查詢功能,是以非常受到歡迎,很多項目都考慮用MongoDB來替代MySQL來實作不是 特别複雜的Web應用,比方說why we migrated from MySQL to MongoDB就是一個真實的從MySQL遷移到MongoDB的案例,由于資料量實在太大,是以遷移到了Mongo上面,資料查詢的速度得到了非常顯著 的提升。

  MongoDB也有一個ruby的項目MongoMapper,是模仿Merb的DataMapper編寫的MongoDB的接口,使用起來非常簡單,幾乎和DataMapper一模一樣,功能非常強大易用。

  MongoDB文法:

  啟動服務

  mongod.exe –dbpath F:\DataBase\MongoDB\db\

  –dbpath 資料檔案存放路徑

  –port 資料服務端口

  啟動用戶端

  mongo.exe cclove

  cclove 所連接配接的資料庫名稱

  資料庫操作文法

  mongo –path

  db.AddUser(username,password) 添加使用者

  db.auth(usrename,password) 設定資料庫連接配接驗證

  db.cloneDataBase(fromhost) 從目标伺服器克隆一個資料庫

  db.commandHelp(name) returns the help for the command

  db.copyDatabase(fromdb,todb,fromhost) 複制資料庫fromdb—源資料庫名稱,todb—目标資料庫名稱,fromhost—源資料庫伺服器位址

  db.createCollection(name,{size:3333,capped:333,max:88888}) 建立一個資料集,相當于一個表

  db.currentOp() 取消目前庫的目前操作

  db.dropDataBase() 删除目前資料庫

  db.eval(func,args) run code server-side

  db.getCollection(cname) 取得一個資料集合,同用法:db['cname'] or db.cname

  db.getCollenctionNames() 取得所有資料集合的名稱清單

  db.getLastError() 傳回最後一個錯誤的提示消息

  db.getLastErrorObj() 傳回最後一個錯誤的對象

  db.getMongo() 取得目前伺服器的連接配接對象get the server connection object

  db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr 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() 停止(殺死)在目前庫的目前操作

  db.printCollectionStats() 傳回目前庫的資料集狀态

  db.printReplicationInfo()

  db.printSlaveReplicationInfo()

  db.printShardingStatus() 傳回目前資料庫是否為共享資料庫

  db.removeUser(username) 删除使用者

  db.repairDatabase() 修複目前資料庫

  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.shutdownServer() 關閉目前服務程式

  db.version() 傳回目前程式的版本資訊

  資料集(表)操作文法

  db.linlin.find({id:10}) 傳回linlin資料集ID=10的資料集

  db.linlin.find({id:10}).count() 傳回linlin資料集ID=10的資料總數

  db.linlin.find({id:10}).limit(2) 傳回linlin資料集ID=10的資料集從第二條開始的資料集

  db.linlin.find({id:10}).skip(8) 傳回linlin資料集ID=10的資料集從0到第八條的資料集

  db.linlin.find({id:10}).limit(2).skip(8) 傳回linlin資料集ID=1=的資料集從第二條到第八條的資料

  db.linlin.find({id:10}).sort() 傳回linlin資料集ID=10的排序資料集

  db.linlin.findOne([query]) 傳回符合條件的一條資料

  db.linlin.getDB() 傳回此資料集所屬的資料庫名稱

  db.linlin.getIndexes() 傳回些資料集的索引資訊

  db.linlin.group({key:…,initial:…,reduce:…[,cond:...]})

  db.linlin.mapReduce(mayFunction,reduceFunction,)

  db.linlin.remove(query) 在資料集中删除一條資料

  db.linlin.renameCollection(newName) 重命名些資料集名稱

  db.linlin.save(obj) 往資料集中插入一條資料

  db.linlin.stats() 傳回此資料集的狀态

  db.linlin.storageSize() 傳回此資料集的存儲大小

  db.linlin.totalIndexSize() 傳回此資料集的索引檔案大小

  db.linlin.totalSize() 傳回些資料集的總大小

  db.linlin.update(query,object[,upsert_bool]) 在此資料集中更新一條資料

  db.linlin.validate() 驗證此資料集

  db.linlin.getShardVersion() 傳回資料集共享版本号

  db.linlin.find({‘name’:'foobar’}) select * from linlin where name=’foobar’

  db.linlin.find() select * from linlin

  db.linlin.find({‘ID’:10}).count() select count(*) from linlin where ID=10

  db.linlin.find().skip(10).limit(20) 從查詢結果的第十條開始讀20條資料 select * from linlin limit 10,20 ———-mysql

  db.linlin.find({‘ID’:{$in:[25,35,45]}}) select * from linlin where ID in (25,35,45)

  db.linlin.find().sort({‘ID’:-1}) select * from linlin order by ID desc

  db.linlin.distinct(‘name’,{‘ID’:{$lt:20}}) select distinct(name) from linlin where ID<20

  db.linlin.group({key:{‘name’:true},cond:{‘name’:'foo’},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})

  select name,sum(marks) from linlin group by name

  db.linlin.find(‘this.ID<20′,{name:1}) select name from linlin where ID<20

  db.linlin.insert({‘name’:'foobar’,'age’:25}) insert into linlin (‘name’,'age’) values(‘foobar’,25)

  db.linlin.insert({‘name’:'foobar’,'age’:25,’email’:'[email protected]’})

  db.linlin.remove({}) delete * from linlin

  db.linlin.remove({‘age’:20}) delete linlin where age=20

  db.linlin.remove({‘age’:{$lt:20}}) delete linlin where age<20

  db.linlin.remove({‘age’:{$lte:20}}) delete linlin where age<=20

  db.linlin.remove({‘age’:{$gt:20}}) delete linlin where age>20

  db.linlin.remove({‘age’:{$gte:20}}) delete linlin where age>=20

  db.linlin.remove({‘age’:{$ne:20}}) delete linlin where age!=20

  db.linlin.update({‘name’:'foobar’},{$set:{‘age’:36}}) update linlin set age=36 where name=’foobar’

  db.linlin.update({‘name’:'foobar’},{$inc:{‘age’:3}}) update linlin set age=age+3 where name=’foobar’