從簡單的mongodb example 的觀察

來源:互聯網
上載者:User

標籤: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 URLvar 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 ServerMongoClient.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 ServerMongoClient.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 的觀察

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.