码迷,mamicode.com
首页 > 数据库 > 详细

从简单的mongodb example 的观察

时间:2014-11-20 23:22:20      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   ar   os   使用   sp   java   

https://github.com/no7dw/mongodb-example

这是最基础的连接查询。(branch master)

var MongoClient = require(‘mongodb‘).MongoClient
  , assert = require(‘assert‘);

// Connection URL
var url = ‘mongodb://localhost:27017/demo‘;

var findaddr = function(db, callback) {
  // Get the addr collection
  var collection = db.collection(‘addr‘);
  // Find some addr
  collection.find({}).toArray(function(err, docs) {
    
    callback(err, docs);
  });      
}

// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected correctly to server");
  findaddr(db,function(err, docs){
  	if(err)
  		console.log(‘we get err‘, err);
  	else
  		console.log(‘result:‘, docs);
  	db.close();
  });
  
});

  

留意到log:

2014-11-20T22:15:52.348+0800 [initandlisten] connection accepted from 127.0.0.1:50076 #39 (1 connection now open)
2014-11-20T22:15:52.384+0800 [conn39] end connection 127.0.0.1:50076 (0 connections now open)
2014-11-20T22:15:52.425+0800 [initandlisten] connection accepted from 127.0.0.1:50077 #40 (1 connection now open)
2014-11-20T22:15:52.426+0800 [initandlisten] connection accepted from 127.0.0.1:50078 #41 (2 connections now open)
2014-11-20T22:15:52.426+0800 [initandlisten] connection accepted from 127.0.0.1:50079 #42 (3 connections now open)
2014-11-20T22:15:52.427+0800 [initandlisten] connection accepted from 127.0.0.1:50080 #43 (4 connections now open)
2014-11-20T22:15:52.428+0800 [initandlisten] connection accepted from 127.0.0.1:50081 #44 (5 connections now open)
2014-11-20T22:15:52.455+0800 [conn40] end connection 127.0.0.1:50077 (4 connections now open)
2014-11-20T22:15:52.455+0800 [conn41] end connection 127.0.0.1:50078 (3 connections now open)
2014-11-20T22:15:52.455+0800 [conn42] end connection 127.0.0.1:50079 (2 connections now open)
2014-11-20T22:15:52.456+0800 [conn43] end connection 127.0.0.1:50080 (1 connection now open)
2014-11-20T22:15:52.456+0800 [conn44] end connection 127.0.0.1:50081 (0 connections now open)

  

原因:

默认mongodb 使用connection poolsize =5 的设置

所以把他根据业务来设置大小。(branch less-connection)

var options = {
  db: { native_parser: true },
  server: { poolSize: 1 }
}

// Use connect method to connect to the Server
MongoClient.connect(url, options, function(err, db) {
  assert.equal(null, err);
  console.log("Connected correctly to server");
  findaddr(db,function(err, docs){
  	if(err)
  		console.log(‘we get err‘, err);
  	else
  		console.log(‘result:‘, docs);
  	db.close();
  });
  
});

  

now see mongd log:

2014-11-20T22:21:46.243+0800 [initandlisten] connection accepted from 127.0.0.1:50157 #45 (1 connection now open)
2014-11-20T22:21:46.255+0800 [conn45] end connection 127.0.0.1:50157 (0 connections now open)
2014-11-20T22:21:46.266+0800 [initandlisten] connection accepted from 127.0.0.1:50158 #46 (1 connection now open)
2014-11-20T22:21:46.278+0800 [conn46] end connection 127.0.0.1:50158 (0 connections now open)

  

seems good. However, 这样设置不能根据业务压力来调整,even 有 maxPoolSize, minPoolSize :

 

uri.maxPoolSize

The maximum number of connections in the connection pool. The default value is 100.

uri.minPoolSize

The minimum number of connections in the connection pool. The default value is 0.

 --- 答案是: 

generic-pool

 

从简单的mongodb example 的观察

标签:des   blog   http   io   ar   os   使用   sp   java   

原文地址:http://www.cnblogs.com/no7dw/p/4111659.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!