设为首页 收藏本站
查看: 973|回复: 0

[经验分享] mongodb有关的研究

[复制链接]

尚未签到

发表于 2015-7-5 13:43:24 | 显示全部楼层 |阅读模式
  mongodb是一款文档型的非关系型数据库,性能非常高,老赵做过相关测试,我测试下来也差不多,和sql server相比,写入性能好太多了,下面是我的测试结果:
  
  一条记录4K,1000万的数据占50G磁盘包括索引
  Document doc = new Document();
  doc.Add("Name", "Joseph" + DateTime.Now.Second);  索引
  doc.Add("Age", r.Next(20, 50));  索引
  doc.Add("Time", DateTime.Now);
  doc.Add("Data", s);
  记录很少的时候写吞吐5000左右
DSC0000.jpg
  记录很少的时候基于索引的搜索每秒2000左右
DSC0001.jpg
  数据很少的时候读吞吐每秒1400
DSC0002.jpg
  数据很少的时候4M数据获取大概237毫秒
  数据很少的时候搜索对写入影响不大
DSC0003.jpg
  数据很少的时候读写相互影响不大
DSC0004.jpg
  数据很多的时候
  写入速度下降一半
DSC0005.jpg
  读取速度没区别
DSC0006.jpg
DSC0007.jpg
  搜索慢了很多,千万数据200毫秒的搜索速度,2个字段
DSC0008.jpg
  读取对写入影响还是不大
DSC0009.jpg
  查询对写入影响很大
DSC00010.jpg
  另 不管数据量多少CPU始终在10%以下,内存随着数据量增多从20% - 80%
  
  查询:
  Document doc = new Document();   
doc.Add("Name", "Joseph10");     
doc.Add("Age", new Document().Append("$gte", 30).Append("$lte", 40));
  
  读取:
  table.FindOne(new Document());
  
  测试程序点击这里下载。测试基于单个master的mongodb,后续测试发现,如果开启master/slave的话,写入速度非常不稳定,在向slave传送数据的时候写入速度下降50%到80%。还有就是mongodb随着并发的上升并没有带来写入速度上的提高,反而下降了一点,可能是因为读写锁的关系。
  
  另外,点击这里下载一个查看mongodb对象的小工具(基于MongoDB.Driver.dll):
  
  列出服务器
DSC00011.png
  列出数据库 表 索引
DSC00012.png
  服务器属性
DSC00013.png
  数据库属性
DSC00014.png
  表属性
DSC00015.png
  列数据
DSC00016.png
  通过写这个工具我觉得,Document使用树形来表示还是很方便的。
  
  我还在想怎么通过类似sql语句的方式查询mongodb,找到了一个gudusoft.gsqlparser,可以解析各种sql,拿来一用,点击这里下载通过sql语句操作mongodb的测试工具,这个工具还是非常原始的版本,只能进行最简单的crud,select不支持group having等子句:
DSC00017.png
  
  其实把这些小工具组合在一起就是一个简单的mongodb客户管理工具。
  在 http://www.mongodb.org/display/DOCS/List+of+Database+Commands 上作者列出了所有db的命令,我们可以通过提交command操作。
  在 http://www.mongodb.org/display/DOCS/DBA+Operations+from+the+Shell 上作者列出了可以在mongod工具中做的一些操作,其实这些操作完全可以通过提交command来进行,因为mongod工具完全就是一个javascript写的,我们看一下源代码中的mongo_vstudio.cpp:
  
const char * jsconcatcode =
"__quiet = false;\n"
"chatty = function(s){\n"
"if ( ! __quiet )\n"
"print( s );}\n"
"friendlyEqual = function( a , b ){\n"
"if ( a == b )\n"
"return true;\n"
"if ( tojson( a ) == tojson( b ) )\n"
"return true;\n"
"return false;}\n"
"doassert = function( msg ){\n"
"print( \"assert: \" + msg );\n"
"throw msg;}\n"
"assert = function( b , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"if ( b )\n"
"return;\n"
"doassert( \"assert failed : \" + msg );}\n"
"assert._debug = false;\n"
"assert.eq = function( a , b , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"if ( a == b )\n"
"return;\n"
"if ( ( a != null && b != null ) && friendlyEqual( a , b ) )\n"
"return;\n"
"doassert( \"[\" + tojson( a ) + \"] != [\" + tojson( b ) + \"] are not equal : \" + msg );}\n"
"assert.neq = function( a , b , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"if ( a != b )\n"
"return;\n"
"doassert( \"[\" + a + \"] != [\" + b + \"] are equal : \" + msg );}\n"
"assert.soon = function( f, msg, timeout, interval ) {\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"var start = new Date();\n"
"timeout = timeout || 30000;\n"
"interval = interval || 200;\n"
"var last;\n"
"while( 1 ) {\n"
"if ( typeof( f ) == \"string\" ){\n"
"if ( eval( f ) )\n"
"return;}\n"
"else {\n"
"if ( f() )\n"
"return;}\n"
"if ( ( new Date() ).getTime() - start.getTime() > timeout )\n"
"doassert( \"assert.soon failed: \" + f + \", msg:\" + msg );\n"
"sleep( interval );}}\n"
"assert.throws = function( func , params , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"try {\n"
"func.apply( null , params );}\n"
"catch ( e ){\n"
"return e;}\n"
"doassert( \"did not throw exception: \" + msg );}\n"
"assert.commandWorked = function( res , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"if ( res.ok == 1 )\n"
"return;\n"
"doassert( \"command failed: \" + tojson( res ) + \" : \" + msg );}\n"
"assert.commandFailed = function( res , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"if ( res.ok == 0 )\n"
"return;\n"
"doassert( \"command worked when it should have failed: \" + tojson( res ) + \" : \" + msg );}\n"
"assert.isnull = function( what , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"if ( what == null )\n"
"return;\n"
"doassert( \"supposed to null (\" + ( msg || \"\" ) + \") was: \" + tojson( what ) );}\n"
"assert.lt = function( a , b , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
&quot;if ( a < b )\n&quot;
&quot;return;\n&quot;
&quot;doassert( a + \&quot; is not less than \&quot; + b + \&quot; : \&quot; + msg );}\n&quot;
&quot;assert.gt = function( a , b , msg ){\n&quot;
&quot;if ( assert._debug && msg ) print( \&quot;in assert for: \&quot; + msg );\n&quot;
&quot;if ( a > b )\n&quot;
&quot;return;\n&quot;
&quot;doassert( a + \&quot; is not greater than \&quot; + b + \&quot; : \&quot; + msg );}\n&quot;
&quot;assert.close = function( a , b , msg ){\n&quot;
&quot;var diff = Math.abs( (a-b)/((a+b)/2) );\n&quot;
&quot;if ( diff < .001 )\n&quot;
&quot;return;\n&quot;
&quot;doassert( a + \&quot; is not close to \&quot; + b + \&quot; diff: \&quot; + diff + \&quot; : \&quot; + msg );}\n&quot;
&quot;Object.extend = function( dst , src , deep ){\n&quot;
&quot;for ( var k in src ){\n&quot;
&quot;var v = src[k];\n&quot;
&quot;if ( deep && typeof(v) == \&quot;object\&quot; ){\n&quot;
&quot;v = Object.extend( typeof ( v.length ) == \&quot;number\&quot; ? [] : {} , v , true );}\n&quot;
&quot;dst[k] = v;}\n&quot;
&quot;return dst;}\n&quot;
&quot;argumentsToArray = function( a ){\n&quot;
&quot;var arr = [];\n&quot;
&quot;for ( var i=0; i>>>>>> skipping \&quot; + x.name);\n&quot;
&quot;return;}\n&quot;
&quot;params[ i % n ].push( x.name );\n&quot;
&quot;++i;}\n&quot;
&quot;);\n&quot;
&quot;params[ 0 ] = Array.shuffle( params[ 0 ] );\n&quot;
&quot;for( var i in params ) {\n&quot;
&quot;params[ i ].unshift( i );}\n&quot;
&quot;return params;}\n&quot;
&quot;ParallelTester.fileTester = function() {\n&quot;
&quot;var args = argumentsToArray( arguments );\n&quot;
&quot;var suite = args.shift();\n&quot;
&quot;args.forEach(\n&quot;
&quot;function( x ) {\n&quot;
&quot;print(\&quot;         S\&quot; + suite + \&quot; Test : \&quot; + x + \&quot; ...\&quot;);\n&quot;
&quot;var time = Date.timeFunc( function() { load(x); }, 1);\n&quot;
&quot;print(\&quot;         S\&quot; + suite + \&quot; Test : \&quot; + x + \&quot; \&quot; + time + \&quot;ms\&quot; );}\n&quot;
&quot;);}\n&quot;
&quot;assert.parallelTests = function( params, msg, newScopes ) {\n&quot;
&quot;newScopes = newScopes || false;\n&quot;
&quot;var wrapper = function( fun, argv ) {\n&quot;
&quot;eval (\n&quot;
&quot;\&quot;var z = function() {\&quot; +\n&quot;
&quot;\&quot;var __parallelTests__fun = \&quot; + fun.toString() + \&quot;;\&quot; +\n&quot;
&quot;\&quot;var __parallelTests__argv = \&quot; + tojson( argv ) + \&quot;;\&quot; +\n&quot;
&quot;\&quot;var __parallelTests__passed = false;\&quot; +\n&quot;
&quot;\&quot;try {\&quot; +\n&quot;
&quot;\&quot;__parallelTests__fun.apply( 0, __parallelTests__argv );\&quot; +\n&quot;
&quot;\&quot;__parallelTests__passed = true;\&quot; +\n&quot;
&quot;\&quot;} catch ( e ) {\&quot; +\n&quot;
&quot;\&quot;print( e );\&quot; +\n&quot;
&quot;\&quot;}\&quot; +\n&quot;
&quot;\&quot;return __parallelTests__passed;\&quot; +\n&quot;
&quot;\&quot;}\&quot;\n&quot;
&quot;);\n&quot;
&quot;return z;}\n&quot;
&quot;var runners = new Array();\n&quot;
&quot;for( var i in params ) {\n&quot;
&quot;var param = params[ i ];\n&quot;
&quot;var test = param.shift();\n&quot;
&quot;var t;\n&quot;
&quot;if ( newScopes )\n&quot;
&quot;t = new ScopedThread( wrapper( test, param ) );\n&quot;
&quot;else\n&quot;
&quot;t = new Thread( wrapper( test, param ) );\n&quot;
&quot;runners.push( t );}\n&quot;
&quot;runners.forEach( function( x ) { x.start(); } );\n&quot;
&quot;var nFailed = 0;\n&quot;
&quot;runners.forEach( function( x ) { if( !x.returnData() ) { ++nFailed; } } );\n&quot;
&quot;assert.eq( 0, nFailed, msg );}}\n&quot;
&quot;tojson = function( x, indent , nolint ){\n&quot;
&quot;if ( x === null )\n&quot;
&quot;return \&quot;null\&quot;;\n&quot;
&quot;if ( x === undefined )\n&quot;
&quot;return \&quot;undefined\&quot;;\n&quot;
&quot;if (!indent)\n&quot;
&quot;indent = \&quot;\&quot;;\n&quot;
&quot;switch ( typeof x ){\n&quot;
&quot;case \&quot;string\&quot;: {\n&quot;
&quot;var s = \&quot;\\\&quot;\&quot;;\n&quot;
&quot;for ( var i=0; i= 0 , \&quot;unexpected error: \&quot; + tojson( e ) );\n&quot;
&quot;print( \&quot;server should be down...\&quot; );}}\n&quot;
&quot;\n&quot;
&quot;DB.prototype.cloneDatabase = function(from) {\n&quot;
&quot;assert( isString(from) && from.length );\n&quot;
&quot;return this._dbCommand( { clone: from } );}\n&quot;
&quot;\n&quot;
&quot;DB.prototype.cloneCollection = function(from, collection, query) {\n&quot;
&quot;assert( isString(from) && from.length );\n&quot;
&quot;assert( isString(collection) && collection.length );\n&quot;
&quot;collection = this._name + \&quot;.\&quot; + collection;\n&quot;
&quot;query = query || {};\n&quot;
&quot;return this._dbCommand( { cloneCollection:collection, from:from, query:query } );}\n&quot;
&quot;\n&quot;
&quot;DB.prototype.copyDatabase = function(fromdb, todb, fromhost, username, password) {\n&quot;
&quot;assert( isString(fromdb) && fromdb.length );\n&quot;
&quot;assert( isString(todb) && todb.length );\n&quot;
&quot;fromhost = fromhost || \&quot;\&quot;;\n&quot;
&quot;if ( username && password ) {\n&quot;
&quot;var n = this._adminCommand( { copydbgetnonce : 1, fromhost:fromhost } );\n&quot;
&quot;return this._adminCommand( { copydb:1, fromhost:fromhost, fromdb:fromdb, todb:todb, username:username, nonce:n.nonce, key:this.__pwHash( n.nonce, username, password ) } );\n&quot;
&quot;} else {\n&quot;
&quot;return this._adminCommand( { copydb:1, fromhost:fromhost, fromdb:fromdb, todb:todb } );}}\n&quot;
&quot;\n&quot;
&quot;DB.prototype.repairDatabase = function() {\n&quot;
&quot;return this._dbCommand( { repairDatabase: 1 } );}\n&quot;
&quot;DB.prototype.help = function() {\n&quot;
&quot;print(\&quot;DB methods:\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.addUser(username, password[,])\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.auth(username, password)\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.cloneDatabase(fromhost)\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.commandHelp(name) returns the help for the command\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.copyDatabase(fromdb, todb, fromhost)\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.createCollection(name, { size : ..., capped : ..., max : ... } )\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.currentOp() displays the current operation in the db\&quot; );\n&quot;
&quot;print(\&quot;\\tdb.dropDatabase()\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.eval(func, args) run code server-side\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.getCollection(cname) same as db['cname'] or db.cname\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.getCollectionNames()\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.getLastError() - just returns the err msg string\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.getLastErrorObj() - return full status object\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.getMongo() get the server connection object\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.getMongo().setSlaveOk() allow this connection to read from the nonmaster member of a replica pair\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.getName()\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.getPrevError()\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.getProfilingLevel()\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.getReplicationInfo()\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.getSisterDB(name) get the db at the same server as this onew\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.killOp(opid) kills the current operation in the db\&quot; );\n&quot;
&quot;print(\&quot;\\tdb.printCollectionStats()\&quot; );\n&quot;
&quot;print(\&quot;\\tdb.printReplicationInfo()\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.printSlaveReplicationInfo()\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.printShardingStatus()\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.removeUser(username)\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.repairDatabase()\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.resetError()\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.setProfilingLevel(level,) 0=off 1=slow 2=all\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.shutdownServer()\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.stats()\&quot;);\n&quot;
&quot;print(\&quot;\\tdb.version() current version of the server\&quot; );}\n&quot;
&quot;DB.prototype.printCollectionStats = function(){\n&quot;
&quot;var mydb = this;\n&quot;
&quot;this.getCollectionNames().forEach(\n&quot;
&quot;function(z){\n&quot;
&quot;print( z );\n&quot;
&quot;printjson( mydb.getCollection(z).stats() );\n&quot;
&quot;print( \&quot;---\&quot; );}\n&quot;
&quot;);}\n&quot;
&quot;\n&quot;
&quot;DB.prototype.setProfilingLevel = function(level,slowms) {\n&quot;
&quot;if (level < 0 || level > 2) {\n&quot;
&quot;throw { dbSetProfilingException : \&quot;input level \&quot; + level + \&quot; is out of range [0..2]\&quot; };}\n&quot;
&quot;var cmd = { profile: level };\n&quot;
&quot;if ( slowms )\n&quot;
&quot;cmd[\&quot;slowms\&quot;] = slowms;\n&quot;
&quot;return this._dbCommand( cmd );}\n&quot;
&quot;\n&quot;
&quot;DB.prototype.eval = function(jsfunction) {\n&quot;
&quot;var cmd = { $eval : jsfunction };\n&quot;
&quot;if ( arguments.length > 1 ) {\n&quot;
&quot;cmd.args = argumentsToArray( arguments ).slice(1);}\n&quot;
&quot;var res = this._dbCommand( cmd );\n&quot;
&quot;if (!res.ok)\n&quot;
&quot;throw tojson( res );\n&quot;
&quot;return res.retval;}\n&quot;
&quot;DB.prototype.dbEval = DB.prototype.eval;\n&quot;
&quot;\n&quot;
&quot;DB.prototype.groupeval = function(parmsObj) {\n&quot;
&quot;var groupFunction = function() {\n&quot;
&quot;var parms = args[0];\n&quot;
&quot;var c = db[parms.ns].find(parms.cond||{});\n&quot;
&quot;var map = new Map();\n&quot;
&quot;var pks = parms.key ? Object.keySet( parms.key ) : null;\n&quot;
&quot;var pkl = pks ? pks.length : 0;\n&quot;
&quot;var key = {};\n&quot;
&quot;while( c.hasNext() ) {\n&quot;
&quot;var obj = c.next();\n&quot;
&quot;if ( pks ) {\n&quot;
&quot;for( var i=0; i= 0 && name != \&quot;local.oplog.$main\&quot; )\n&quot;
&quot;return;\n&quot;
&quot;all.push( name.substring( nsLength ) );}\n&quot;
&quot;);\n&quot;
&quot;return all;}\n&quot;
&quot;DB.prototype.tojson = function(){\n&quot;
&quot;return this._name;}\n&quot;
&quot;DB.prototype.toString = function(){\n&quot;
&quot;return this._name;}\n&quot;
&quot;DB.prototype.currentOp = function(){\n&quot;
&quot;return db.$cmd.sys.inprog.findOne();}\n&quot;
&quot;DB.prototype.currentOP = DB.prototype.currentOp;\n&quot;
&quot;DB.prototype.killOp = function(op) {\n&quot;
&quot;if( !op )\n&quot;
&quot;throw \&quot;no opNum to kill specified\&quot;;\n&quot;
&quot;return db.$cmd.sys.killop.findOne({'op':op});}\n&quot;
&quot;DB.prototype.killOP = DB.prototype.killOp;\n&quot;
&quot;DB.tsToSeconds = function(x){\n&quot;
&quot;if ( x.t && x.i )\n&quot;
&quot;return x.t / 1000;\n&quot;
&quot;return x / 4294967296;}\n&quot;
&quot;\n&quot;
&quot;DB.prototype.getReplicationInfo = function() {\n&quot;
&quot;var db = this.getSisterDB(\&quot;local\&quot;);\n&quot;
&quot;var result = { };\n&quot;
&quot;var ol = db.system.namespaces.findOne({name:\&quot;local.oplog.$main\&quot;});\n&quot;
&quot;if( ol && ol.options ) {\n&quot;
&quot;result.logSizeMB = ol.options.size / 1000 / 1000;\n&quot;
&quot;} else {\n&quot;
&quot;result.errmsg  = \&quot;local.oplog.$main, or its options, not found in system.namespaces collection (not --master?)\&quot;;\n&quot;
&quot;return result;}\n&quot;
&quot;var firstc = db.oplog.$main.find().sort({$natural:1}).limit(1);\n&quot;
&quot;var lastc = db.oplog.$main.find().sort({$natural:-1}).limit(1);\n&quot;
&quot;if( !firstc.hasNext() || !lastc.hasNext() ) {\n&quot;
&quot;result.errmsg = \&quot;objects not found in local.oplog.$main -- is this a new and empty db instance?\&quot;;\n&quot;
&quot;result.oplogMainRowCount = db.oplog.$main.count();\n&quot;
&quot;return result;}\n&quot;
&quot;var first = firstc.next();\n&quot;
&quot;var last = lastc.next();\n&quot;
&quot;{\n&quot;
&quot;var tfirst = first.ts;\n&quot;
&quot;var tlast = last.ts;\n&quot;
&quot;if( tfirst && tlast ) {\n&quot;
&quot;tfirst = DB.tsToSeconds( tfirst );\n&quot;
&quot;tlast = DB.tsToSeconds( tlast );\n&quot;
&quot;result.timeDiff = tlast - tfirst;\n&quot;
&quot;result.timeDiffHours = Math.round(result.timeDiff / 36)/100;\n&quot;
&quot;result.tFirst = (new Date(tfirst*1000)).toString();\n&quot;
&quot;result.tLast  = (new Date(tlast*1000)).toString();\n&quot;
&quot;result.now = Date();}\n&quot;
&quot;else {\n&quot;
&quot;result.errmsg = \&quot;ts element not found in oplog objects\&quot;;}}\n&quot;
&quot;return result;}\n&quot;
&quot;DB.prototype.printReplicationInfo = function() {\n&quot;
&quot;var result = this.getReplicationInfo();\n&quot;
&quot;if( result.errmsg ) {\n&quot;
&quot;print(tojson(result));\n&quot;
&quot;return;}\n&quot;
&quot;print(\&quot;configured oplog size:   \&quot; + result.logSizeMB + \&quot;MB\&quot;);\n&quot;
&quot;print(\&quot;log length start to end: \&quot; + result.timeDiff + \&quot;secs (\&quot; + result.timeDiffHours + \&quot;hrs)\&quot;);\n&quot;
&quot;print(\&quot;oplog first event time:  \&quot; + result.tFirst);\n&quot;
&quot;print(\&quot;oplog last event time:   \&quot; + result.tLast);\n&quot;
&quot;print(\&quot;now:                     \&quot; + result.now);}\n&quot;
&quot;DB.prototype.printSlaveReplicationInfo = function() {\n&quot;
&quot;function g(x) {\n&quot;
&quot;print(\&quot;source:   \&quot; + x.host);\n&quot;
&quot;var st = new Date( DB.tsToSeconds( x.syncedTo ) * 1000 );\n&quot;
&quot;var now = new Date();\n&quot;
&quot;print(\&quot;syncedTo: \&quot; + st.toString() );\n&quot;
&quot;var ago = (now-st)/1000;\n&quot;
&quot;var hrs = Math.round(ago/36)/100;\n&quot;
&quot;print(\&quot;          = \&quot; + Math.round(ago) + \&quot;secs ago (\&quot; + hrs + \&quot;hrs)\&quot;);}\n&quot;
&quot;var L = this.getSisterDB(\&quot;local\&quot;);\n&quot;
&quot;if( L.sources.count() == 0 ) {\n&quot;
&quot;print(\&quot;local.sources is empty; is this db a --slave?\&quot;);\n&quot;
&quot;return;}\n&quot;
&quot;L.sources.find().forEach(g);}\n&quot;
&quot;DB.prototype.serverBuildInfo = function(){\n&quot;
&quot;return this._adminCommand( \&quot;buildinfo\&quot; );}\n&quot;
&quot;DB.prototype.serverStatus = function(){\n&quot;
&quot;return this._adminCommand( \&quot;serverStatus\&quot; );}\n&quot;
&quot;DB.prototype.version = function(){\n&quot;
&quot;return this.serverBuildInfo().version;}\n&quot;
&quot;DB.prototype.printShardingStatus = function(){\n&quot;
&quot;printShardingStatus( this.getSisterDB( \&quot;config\&quot; ) );}\n&quot;
&quot;if ( typeof Mongo == \&quot;undefined\&quot; ){\n&quot;
&quot;Mongo = function( host ){\n&quot;
&quot;this.init( host );}}\n&quot;
&quot;if ( ! Mongo.prototype ){\n&quot;
&quot;throw \&quot;Mongo.prototype not defined\&quot;;}\n&quot;
&quot;if ( ! Mongo.prototype.find )\n&quot;
&quot;Mongo.prototype.find = function( ns , query , fields , limit , skip ){ throw \&quot;find not implemented\&quot;; }\n&quot;
&quot;if ( ! Mongo.prototype.insert )\n&quot;
&quot;Mongo.prototype.insert = function( ns , obj ){ throw \&quot;insert not implemented\&quot;; }\n&quot;
&quot;if ( ! Mongo.prototype.remove )\n&quot;
&quot;Mongo.prototype.remove = function( ns , pattern ){ throw \&quot;remove not implemented;\&quot; }\n&quot;
&quot;if ( ! Mongo.prototype.update )\n&quot;
&quot;Mongo.prototype.update = function( ns , query , obj , upsert ){ throw \&quot;update not implemented;\&quot; }\n&quot;
&quot;if ( typeof mongoInject == \&quot;function\&quot; ){\n&quot;
&quot;mongoInject( Mongo.prototype );}\n&quot;
&quot;Mongo.prototype.setSlaveOk = function() {\n&quot;
&quot;this.slaveOk = true;}\n&quot;
&quot;Mongo.prototype.getDB = function( name ){\n&quot;
&quot;return new DB( this , name );}\n&quot;
&quot;Mongo.prototype.getDBs = function(){\n&quot;
&quot;var res = this.getDB( \&quot;admin\&quot; ).runCommand( { \&quot;listDatabases\&quot; : 1 } );\n&quot;
&quot;assert( res.ok == 1 , \&quot;listDatabases failed:\&quot; + tojson( res ) );\n&quot;
&quot;return res;}\n&quot;
&quot;Mongo.prototype.getDBNames = function(){\n&quot;
&quot;return this.getDBs().databases.map(\n&quot;
&quot;function(z){\n&quot;
&quot;return z.name;}\n&quot;
&quot;);}\n&quot;
&quot;Mongo.prototype.getCollection = function(ns){\n&quot;
&quot;var idx = ns.indexOf( \&quot;.\&quot; );\n&quot;
&quot;if ( idx < 0 )\n&quot;
&quot;throw \&quot;need . in ns\&quot;;\n&quot;
&quot;var db = ns.substring( 0 , idx );\n&quot;
&quot;var c = ns.substring( idx + 1 );\n&quot;
&quot;return this.getDB( db ).getCollection( c );}\n&quot;
&quot;Mongo.prototype.toString = function(){\n&quot;
&quot;return \&quot;mongo connection to \&quot; + this.host;}\n&quot;
&quot;connect = function( url , user , pass ){\n&quot;
&quot;chatty( \&quot;connecting to: \&quot; + url )\n&quot;
&quot;if ( user && ! pass )\n&quot;
&quot;throw \&quot;you specified a user and not a password.  either you need a password, or you're using the old connect api\&quot;;\n&quot;
&quot;var idx = url.indexOf( \&quot;/\&quot; );\n&quot;
&quot;var db;\n&quot;
&quot;if ( idx < 0 )\n&quot;
&quot;db = new Mongo().getDB( url );\n&quot;
&quot;else\n&quot;
&quot;db = new Mongo( url.substring( 0 , idx ) ).getDB( url.substring( idx + 1 ) );\n&quot;
&quot;if ( user && pass ){\n&quot;
&quot;if ( ! db.auth( user , pass ) ){\n&quot;
&quot;throw \&quot;couldn't login\&quot;;}}\n&quot;
&quot;return db;}\n&quot;
&quot;MR = {};\n&quot;
&quot;MR.init = function(){\n&quot;
&quot;$max = 0;\n&quot;
&quot;$arr = [];\n&quot;
&quot;emit = MR.emit;\n&quot;
&quot;$numEmits = 0;\n&quot;
&quot;$numReduces = 0;\n&quot;
&quot;$numReducesToDB = 0;\n&quot;
&quot;gc();}\n&quot;
&quot;MR.cleanup = function(){\n&quot;
&quot;MR.init();\n&quot;
&quot;gc();}\n&quot;
&quot;MR.emit = function(k,v){\n&quot;
&quot;$numEmits++;\n&quot;
&quot;var num = nativeHelper.apply( get_num_ , [ k ] );\n&quot;
&quot;var data = $arr[num];\n&quot;
&quot;if ( ! data ){\n&quot;
&quot;data = { key : k , values : new Array(1000) , count : 0 };\n&quot;
&quot;$arr[num] = data;}\n&quot;
&quot;data.values[data.count++] = v;\n&quot;
&quot;$max = Math.max( $max , data.count );}\n&quot;
&quot;MR.doReduce = function( useDB ){\n&quot;
&quot;$numReduces++;\n&quot;
&quot;if ( useDB )\n&quot;
&quot;$numReducesToDB++;\n&quot;
&quot;$max = 0;\n&quot;
&quot;for ( var i=0; i 0 ) {\n&quot;
&quot;ret.unique = true;}\n&quot;
&quot;if ( nTrue > 1 ) {\n&quot;
&quot;ret.dropDups = true;}}\n&quot;
&quot;*/\n&quot;
&quot;return ret;}\n&quot;
&quot;DBCollection.prototype.createIndex = function( keys , options ){\n&quot;
&quot;var o = this._indexSpec( keys, options );\n&quot;
&quot;this._db.getCollection( \&quot;system.indexes\&quot; ).insert( o , true );}\n&quot;
&quot;DBCollection.prototype.ensureIndex = function( keys , options ){\n&quot;
&quot;var name = this._indexSpec( keys, options ).name;\n&quot;
&quot;this._indexCache = this._indexCache || {};\n&quot;
&quot;if ( this._indexCache[ name ] ){\n&quot;
&quot;return;}\n&quot;
&quot;this.createIndex( keys , options );\n&quot;
&quot;if ( this.getDB().getLastError() == \&quot;\&quot; ) {\n&quot;
&quot;this._indexCache[name] = true;}}\n&quot;
&quot;DBCollection.prototype.resetIndexCache = function(){\n&quot;
&quot;this._indexCache = {};}\n&quot;
&quot;DBCollection.prototype.reIndex = function() {\n&quot;
&quot;return this._db.runCommand({ reIndex: this.getName() });}\n&quot;
&quot;DBCollection.prototype.dropIndexes = function(){\n&quot;
&quot;this.resetIndexCache();\n&quot;
&quot;var res = this._db.runCommand( { deleteIndexes: this.getName(), index: \&quot;*\&quot; } );\n&quot;
&quot;assert( res , \&quot;no result from dropIndex result\&quot; );\n&quot;
&quot;if ( res.ok )\n&quot;
&quot;return res;\n&quot;
&quot;if ( res.errmsg.match( /not found/ ) )\n&quot;
&quot;return res;\n&quot;
&quot;throw \&quot;error dropping indexes : \&quot; + tojson( res );}\n&quot;
&quot;DBCollection.prototype.drop = function(){\n&quot;
&quot;this.resetIndexCache();\n&quot;
&quot;var ret = this._db.runCommand( { drop: this.getName() } );\n&quot;
&quot;if ( ! ret.ok ){\n&quot;
&quot;if ( ret.errmsg == \&quot;ns not found\&quot; )\n&quot;
&quot;return false;\n&quot;
&quot;throw \&quot;drop failed: \&quot; + tojson( ret );}\n&quot;
&quot;return true;}\n&quot;
&quot;DBCollection.prototype.findAndModify = function(args){\n&quot;
&quot;var cmd = { findandmodify: this.getName() };\n&quot;
&quot;for (var key in args){\n&quot;
&quot;cmd[key] = args[key];}\n&quot;
&quot;var ret = this._db.runCommand( cmd );\n&quot;
&quot;if ( ! ret.ok ){\n&quot;
&quot;if (ret.errmsg == \&quot;No matching object found\&quot;){\n&quot;
&quot;return {};}\n&quot;
&quot;throw \&quot;findAndModifyFailed failed: \&quot; + tojson( ret.errmsg );}\n&quot;
&quot;return ret.value;}\n&quot;
&quot;DBCollection.prototype.renameCollection = function( newName , dropTarget ){\n&quot;
&quot;return this._db._adminCommand( { renameCollection : this._fullName ,\n&quot;
&quot;to : this._db._name + \&quot;.\&quot; + newName ,\n&quot;
&quot;dropTarget : dropTarget } )}\n&quot;
&quot;DBCollection.prototype.validate = function() {\n&quot;
&quot;var res = this._db.runCommand( { validate: this.getName() } );\n&quot;
&quot;res.valid = false;\n&quot;
&quot;if ( res.result ){\n&quot;
&quot;var str = \&quot;-\&quot; + tojson( res.result );\n&quot;
&quot;res.valid = ! ( str.match( /exception/ ) || str.match( /corrupt/ ) );\n&quot;
&quot;var p = /lastExtentSize:(\\d+)/;\n&quot;
&quot;var r = p.exec( str );\n&quot;
&quot;if ( r ){\n&quot;
&quot;res.lastExtentSize = Number( r[1] );}}\n&quot;
&quot;return res;}\n&quot;
&quot;DBCollection.prototype.getShardVersion = function(){\n&quot;
&quot;return this._db._adminCommand( { getShardVersion : this._fullName } );}\n&quot;
&quot;DBCollection.prototype.getIndexes = function(){\n&quot;
&quot;return this.getDB().getCollection( \&quot;system.indexes\&quot; ).find( { ns : this.getFullName() } ).toArray();}\n&quot;
&quot;DBCollection.prototype.getIndices = DBCollection.prototype.getIndexes;\n&quot;
&quot;DBCollection.prototype.getIndexSpecs = DBCollection.prototype.getIndexes;\n&quot;
&quot;DBCollection.prototype.getIndexKeys = function(){\n&quot;
&quot;return this.getIndexes().map(\n&quot;
&quot;function(i){\n&quot;
&quot;return i.key;}\n&quot;
&quot;);}\n&quot;
&quot;DBCollection.prototype.count = function( x ){\n&quot;
&quot;return this.find( x ).count();}\n&quot;
&quot;\n&quot;
&quot;DBCollection.prototype.clean = function() {\n&quot;
&quot;return this._dbCommand( { clean: this.getName() } );}\n&quot;
&quot;\n&quot;
&quot;DBCollection.prototype.dropIndex =  function(index) {\n&quot;
&quot;assert(index , \&quot;need to specify index to dropIndex\&quot; );\n&quot;
&quot;if ( ! isString( index ) && isObject( index ) )\n&quot;
&quot;index = this._genIndexName( index );\n&quot;
&quot;var res = this._dbCommand( \&quot;deleteIndexes\&quot; ,{ index: index } );\n&quot;
&quot;this.resetIndexCache();\n&quot;
&quot;return res;}\n&quot;
&quot;DBCollection.prototype.copyTo = function( newName ){\n&quot;
&quot;return this.getDB().eval(\n&quot;
&quot;function( collName , newName ){\n&quot;
&quot;var from = db[collName];\n&quot;
&quot;var to = db[newName];\n&quot;
&quot;to.ensureIndex( { _id : 1 } );\n&quot;
&quot;var count = 0;\n&quot;
&quot;var cursor = from.find();\n&quot;
&quot;while ( cursor.hasNext() ){\n&quot;
&quot;var o = cursor.next();\n&quot;
&quot;count++;\n&quot;
&quot;to.save( o );}\n&quot;
&quot;return count;\n&quot;
&quot;} , this.getName() , newName\n&quot;
&quot;);}\n&quot;
&quot;DBCollection.prototype.getCollection = function( subName ){\n&quot;
&quot;return this._db.getCollection( this._shortName + \&quot;.\&quot; + subName );}\n&quot;
&quot;DBCollection.prototype.stats = function( scale ){\n&quot;
&quot;return this._db.runCommand( { collstats : this._shortName , scale : scale } );}\n&quot;
&quot;DBCollection.prototype.dataSize = function(){\n&quot;
&quot;return this.stats().size;}\n&quot;
&quot;DBCollection.prototype.storageSize = function(){\n&quot;
&quot;return this.stats().storageSize;}\n&quot;
&quot;DBCollection.prototype.totalIndexSize = function( verbose ){\n&quot;
&quot;var stats = this.stats();\n&quot;
&quot;if (verbose){\n&quot;
&quot;for (var ns in stats.indexSizes){\n&quot;
&quot;print( ns + \&quot;\\t\&quot; + stats.indexSizes[ns] );}}\n&quot;
&quot;return stats.totalIndexSize;}\n&quot;
&quot;DBCollection.prototype.totalSize = function(){\n&quot;
&quot;var total = this.storageSize();\n&quot;
&quot;var mydb = this._db;\n&quot;
&quot;var shortName = this._shortName;\n&quot;
&quot;this.getIndexes().forEach(\n&quot;
&quot;function( spec ){\n&quot;
&quot;var coll = mydb.getCollection( shortName + \&quot;.$\&quot; + spec.name );\n&quot;
&quot;var mysize = coll.storageSize();\n&quot;
&quot;total += coll.dataSize();}\n&quot;
&quot;);\n&quot;
&quot;return total;}\n&quot;
&quot;DBCollection.prototype.convertToCapped = function( bytes ){\n&quot;
&quot;if ( ! bytes )\n&quot;
&quot;throw \&quot;have to specify # of bytes\&quot;;\n&quot;
&quot;return this._dbCommand( { convertToCapped : this._shortName , size : bytes } )}\n&quot;
&quot;DBCollection.prototype.exists = function(){\n&quot;
&quot;return this._db.system.namespaces.findOne( { name : this._fullName } );}\n&quot;
&quot;DBCollection.prototype.isCapped = function(){\n&quot;
&quot;var e = this.exists();\n&quot;
&quot;return ( e && e.options && e.options.capped ) ? true : false;}\n&quot;
&quot;DBCollection.prototype.distinct = function( keyString , query ){\n&quot;
&quot;var res = this._dbCommand( { distinct : this._shortName , key : keyString , query : query || {} } );\n&quot;
&quot;if ( ! res.ok )\n&quot;
&quot;throw \&quot;distinct failed: \&quot; + tojson( res );\n&quot;
&quot;return res.values;}\n&quot;
&quot;DBCollection.prototype.group = function( params ){\n&quot;
&quot;params.ns = this._shortName;\n&quot;
&quot;return this._db.group( params );}\n&quot;
&quot;DBCollection.prototype.groupcmd = function( params ){\n&quot;
&quot;params.ns = this._shortName;\n&quot;
&quot;return this._db.groupcmd( params );}\n&quot;
&quot;MapReduceResult = function( db , o ){\n&quot;
&quot;Object.extend( this , o );\n&quot;
&quot;this._o = o;\n&quot;
&quot;this._keys = Object.keySet( o );\n&quot;
&quot;this._db = db;\n&quot;
&quot;this._coll = this._db.getCollection( this.result );}\n&quot;
&quot;MapReduceResult.prototype._simpleKeys = function(){\n&quot;
&quot;return this._o;}\n&quot;
&quot;MapReduceResult.prototype.find = function(){\n&quot;
&quot;return DBCollection.prototype.find.apply( this._coll , arguments );}\n&quot;
&quot;MapReduceResult.prototype.drop = function(){\n&quot;
&quot;return this._coll.drop();}\n&quot;
&quot;\n&quot;
&quot;MapReduceResult.prototype.convertToSingleObject = function(){\n&quot;
&quot;var z = {};\n&quot;
&quot;this._coll.find().forEach( function(a){ z[a._id] = a.value; } );\n&quot;
&quot;return z;}\n&quot;
&quot;\n&quot;
&quot;DBCollection.prototype.mapReduce = function( map , reduce , optional ){\n&quot;
&quot;var c = { mapreduce : this._shortName , map : map , reduce : reduce };\n&quot;
&quot;if ( optional )\n&quot;
&quot;Object.extend( c , optional );\n&quot;
&quot;var raw = this._db.runCommand( c );\n&quot;
&quot;if ( ! raw.ok )\n&quot;
&quot;throw \&quot;map reduce failed: \&quot; + tojson( raw );\n&quot;
&quot;return new MapReduceResult( this._db , raw );}\n&quot;
&quot;DBCollection.prototype.toString = function(){\n&quot;
&quot;return this.getFullName();}\n&quot;
&quot;DBCollection.prototype.toString = function(){\n&quot;
&quot;return this.getFullName();}\n&quot;
&quot;DBCollection.prototype.tojson = DBCollection.prototype.toString;\n&quot;
&quot;DBCollection.prototype.shellPrint = DBCollection.prototype.toString;\n&quot;
;

  比如我们知道可以从db shell中查看当前操作,但是不知道怎么在客户端中实现这个功能,可以找到这个文件中的:
  &quot;DB.prototype.currentOp = function(){\n&quot;
&quot;return db.$cmd.sys.inprog.findOne();}\n&quot;
&quot;DB.prototype.currentOP = DB.prototype.currentOp;\n&quot;
&quot;DB.prototype.killOp = function(op) {\n&quot;
&quot;if( !op )\n&quot;
&quot;throw \&quot;no opNum to kill specified\&quot;;\n&quot;
&quot;return db.$cmd.sys.killop.findOne({'op':op});}\n&quot;
&quot;DB.prototype.killOP = DB.prototype.killOp;\n&quot;
  这几句,我想大家就知道怎么实现了吧。
  
  mongodb是一个好东西,即使和sql server 性能差不多也是好东西(何况性能这么好),因为免费/开源,sql server多贵啊。不过呢,使用nosql还是要看应用的,mongodb的数据基于bson,列名都是重复保存的,所以占用的空间比较大一点,不太适合列数很多的数据,如果要保存日志的话甚至可以除了需要索引的字段都以一个字符串来保存。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-83392-1-1.html 上篇帖子: 高可用的MongoDB集群-实战篇 下篇帖子: 第六节 MongoDB 状态监控、备份复制及自动分片
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表