MongoDB基本介紹及一些用法
滿足海量存儲需求和訪問的面向文檔的數據庫:MongoDB,CouchDB
MongoDB
Nice, I like it very much.
面向文檔的非關系數據庫主要解決的問題不是高性能的并發讀寫,而是保證海量數據存儲的同時,具有良好的查詢性能。MongoDB是用C++開發的,而CouchDB則是Erlang開發的:
1、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應用,比方說whywe migrated from MySQL toMongoDB就是一個真實的從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,<optional params>) 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’:'cclove2@163.com’}) 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’