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

2014-11-20T23:35:25.954+0800 [initandlisten] connection accepted from 127.0.0.1:52277 #1 (1 connection now open)
2014-11-20T23:35:25.955+0800 [initandlisten] connection accepted from 127.0.0.1:52278 #2 (2 connections now open)
2014-11-20T23:35:35.877+0800 [clientcursormon] mem (MB) res:67 virt:837
2014-11-20T23:35:35.877+0800 [clientcursormon] mapped (incl journal view):640
2014-11-20T23:35:35.878+0800 [clientcursormon] connections:2
2014-11-20T23:35:56.021+0800 [conn2] end connection 127.0.0.1:52278 (1 connection now open)
2014-11-20T23:35:56.024+0800 [conn1] end connection 127.0.0.1:52277 (0 connections now open)

var http=require('http'),
mongodb = require("mongodb"),
poolModule = require('generic-pool'); var pool = poolModule.Pool({
name : 'mongodb',
create : function(callback) {
var server_options={'auto_reconnect':false,poolSize:1};
//http://docs.mongodb.org/manual/reference/connection-string/#uri.w
var db_options={w:-1};
var mongoserver = new mongodb.Server('localhost', 27017,server_options );
var db=new mongodb.Db('addr', mongoserver, db_options);
db.open(function(err,db){
if(err)return callback(err);
callback(null,db);
});
},
destroy : function(db) { db.close(); },
max : 10,
// optional. if you set this, make sure to drain() (see step 3)
// min : 2,
// specifies how long a resource can stay idle in pool before being removed
idleTimeoutMillis : 30000,
// if true, logs via console.log - can also be a function
log : false
}); var server=http.createServer(function(req,res){
// acquire connection - callback function is called
// once a resource becomes available
pool.acquire(function(err, db) {
if (err) {
res.statusCode=500;
res.end(JSON.stringify(err,null,2));
} else {
db.collection('addr').find({}).toArray(function(err, docs) {
res.end(JSON.stringify(docs,null,2));
// return object back to pool
pool.release(db);
});
}
});
});
server.listen(8080,function(){
console.log('server listen to %d',this.address().port);
}); setTimeout(function(){
for (var i = 0; i < 20; i++) {
//serval second later , connection will close
console.log("request #%d",i);
http.get('http://localhost:8080',function(res){
console.log('request ok')
});
} },2000);

//只开了5个connection ?why?

2014-11-21T00:06:04.342+0800 [initandlisten] connection accepted from 127.0.0.1:52642 #25 (1 connection now open)
2014-11-21T00:06:04.369+0800 [initandlisten] connection accepted from 127.0.0.1:52643 #26 (2 connections now open)
2014-11-21T00:06:04.370+0800 [initandlisten] connection accepted from 127.0.0.1:52644 #27 (3 connections now open)
2014-11-21T00:06:04.370+0800 [initandlisten] connection accepted from 127.0.0.1:52645 #28 (4 connections now open)
2014-11-21T00:06:04.370+0800 [initandlisten] connection accepted from 127.0.0.1:52646 #29 (5 connections now open)
2014-11-21T00:06:34.430+0800 [conn28] end connection 127.0.0.1:52645 (4 connections now open)
2014-11-21T00:06:34.436+0800 [conn29] end connection 127.0.0.1:52646 (3 connections now open)
2014-11-21T00:06:34.437+0800 [conn25] end connection 127.0.0.1:52642 (2 connections now open)
2014-11-21T00:06:34.438+0800 [conn26] end connection 127.0.0.1:52643 (1 connection now open)
2014-11-21T00:06:34.438+0800 [conn27] end connection 127.0.0.1:52644 (0 connections now open)
2014-11-21T00:08:04.517+0800 [initandlisten] connection accepted from 127.0.0.1:52669 #30 (1 connection now open)
2014-11-21T00:08:04.533+0800 [initandlisten] connection accepted from 127.0.0.1:52670 #31 (2 connections now open)
2014-11-21T00:08:04.533+0800 [initandlisten] connection accepted from 127.0.0.1:52671 #32 (3 connections now open)
2014-11-21T00:08:04.539+0800 [initandlisten] connection accepted from 127.0.0.1:52672 #33 (4 connections now open)
2014-11-21T00:08:04.539+0800 [initandlisten] connection accepted from 127.0.0.1:52673 #34 (5 connections now open)
2014-11-21T00:08:34.558+0800 [conn30] end connection 127.0.0.1:52669 (4 connections now open)
2014-11-21T00:08:34.559+0800 [conn31] end connection 127.0.0.1:52670 (3 connections now open)
2014-11-21T00:08:34.560+0800 [conn32] end connection 127.0.0.1:52671 (2 connections now open)
2014-11-21T00:08:34.562+0800 [conn33] end connection 127.0.0.1:52672 (1 connection now open)
2014-11-21T00:08:34.563+0800 [conn34] end connection 127.0.0.1:52673 (0 connections now open)
2014-11-21T00:09:36.124+0800 [clientcursormon] mem (MB) res:38 virt:1002
2014-11-21T00:09:36.124+0800 [clientcursormon] mapped (incl journal view):800
2014-11-21T00:09:36.124+0800 [clientcursormon] connections:0
2014-11-21T00:10:04.634+0800 [initandlisten] connection accepted from 127.0.0.1:52695 #35 (1 connection now open)
2014-11-21T00:10:04.636+0800 [initandlisten] connection accepted from 127.0.0.1:52697 #36 (2 connections now open)
2014-11-21T00:10:04.645+0800 [initandlisten] connection accepted from 127.0.0.1:52698 #37 (3 connections now open)
2014-11-21T00:10:04.649+0800 [initandlisten] connection accepted from 127.0.0.1:52699 #38 (4 connections now open)
2014-11-21T00:10:04.649+0800 [initandlisten] connection accepted from 127.0.0.1:52700 #39 (5 connections now open)
2014-11-21T00:10:34.686+0800 [conn36] end connection 127.0.0.1:52697 (4 connections now open)
2014-11-21T00:10:34.687+0800 [conn35] end connection 127.0.0.1:52695 (3 connections now open)
2014-11-21T00:10:34.689+0800 [conn37] end connection 127.0.0.1:52698 (2 connections now open)
2014-11-21T00:10:34.690+0800 [conn38] end connection 127.0.0.1:52699 (1 connection now open)
2014-11-21T00:10:34.691+0800 [conn39] end connection 127.0.0.1:52700 (0 connections now open)
2014-11-21T00:12:04.773+0800 [initandlisten] connection accepted from 127.0.0.1:52720 #40 (1 connection now open)
2014-11-21T00:12:04.796+0800 [initandlisten] connection accepted from 127.0.0.1:52721 #41 (2 connections now open)
2014-11-21T00:12:04.796+0800 [initandlisten] connection accepted from 127.0.0.1:52722 #42 (3 connections now open)
2014-11-21T00:12:04.797+0800 [initandlisten] connection accepted from 127.0.0.1:52723 #43 (4 connections now open)
2014-11-21T00:12:04.799+0800 [initandlisten] connection accepted from 127.0.0.1:52724 #44 (5 connections now open)

 

output:

request 返回是每5个返回的,其他都还在request 当中。

wade@V1088:~/projects/github/d/mongodb-example$ node genric.js
server listen to 8080
request #0
request #1
request #2
request #3
request #4
request #5
request #6
request #7
request #8
request #9
request #10
request #11
request #12
request #13
request #14
request #15
request #16
request #17
request #18
request #19
request ok
request ok
request ok
request ok
request ok

//...分开时间的
request ok
request ok
request ok
request ok
request ok

//...分开时间的
request ok
request ok
request ok
request ok
request ok

//...分开时间的
request ok

request ok
request ok
request ok
request ok

BUT 只开了5个connection, 而非10个(max) ?why?

EOF

从简单的mongodb example 的观察的更多相关文章

  1. mongoDB介绍、安装、搭建简单的mongoDB服务器(一)

    相关网站 1. http://www.mongodb.org/ 官网,可以下载安装程序,和doc,和驱动等. 2. http://www.mongoing.com/ 国内官方网站,博客,问题谈论等  ...

  2. C# Asp.net中简单操作MongoDB数据库(二)

    C# Asp.net中简单操作MongoDB数据库(一)    , mongodb数据库连接可以回顾上面的篇幅. 1.model类: public class BaseEntity { /// < ...

  3. C# Asp.net中简单操作MongoDB数据库(一)

    需要引用MongoDB.Driver.dll.MongoDB.Driver.core.dll.MongoDB.Bson.dll三个dll. 1.数据库连接: public class MongoDb ...

  4. 简单封装mongodb

    首先安装mongodb  npm i mongodb --save 简单封装,在modules目录下新建db.js var MongoClient=require('mongodb').MongoCl ...

  5. CentOS7+Docker+MangoDB下部署简单的MongoDB分片集群

    简单的在Docker上快速部署MongoDB分片集群 前言 文中使用的环境如下 OS:CentOS Linux release 7.5.1804 (Core) Docker:Docker versio ...

  6. nodejs 简单对mongodb 操作

    路由到了 index.js /* * GET home page. 控制器 */ exports.index = function(req, res){ // res.render('index', ...

  7. window10简单安装MongoDB

    文章参考 在Windows上安装MongoDB 首先,在官网下载安装包.下载地址 内容如下所示: 配置 1. 创建数据目录 E:\MongoDB\data\db 2. 配置环境变量 运行 1. 命令行 ...

  8. windows如何简单安装mongodb

    windows如何安装mongodb 步骤: 1.下载地址 2.选择zip(解压版本) 3.压缩文件解压到  /D:盘 4.在 D:盘  下建一个 data文件夹,data下建 db文件夹:   D: ...

  9. C#简单操作MongoDB

    一 安装MongoDB 官网按需下载, 安装, 一步到位. 二 VS创建新项目 创建一个.netcore console项目, 然后nuget安装驱动MongoDB.Driver 三 建立连接 在Pr ...

随机推荐

  1. css中的position属性值的探究

    css的position属性指定了元素的定位类型,然后通过top,botton,left,right来具体定位. 在具体定位之前必须使用position属性,否则所有的具体定位属性都无法生效. pos ...

  2. xshell提示必须安装最新的更新

    今天大家的xshell基本都出了这个问题 调整时间,调整到比较前的时间,打开xshell即可. 然后工具->选项 把更新去了

  3. 最短路径:Dijkstra & Floyd 算法图解,c++描述

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. (C/C++学习笔记) 二十. 文件和流

    二十. 文件和流 ● 文件的概念 文件(file) 一. C/C++语言将文件作为字节序列(sequence of characters)来对待,但从编码角度,或说从对字节信息的解释来看,文件分为:文 ...

  5. [PyImageSearch] Ubuntu16.04下针对OCR安装Tesseract

    今天的博文是安装和使用光学字符识别(OCR)的Tesseract库的两部分系列的第一部分. 本系列的第一部分将着重于在您的机器上安装和配置Tesseract,然后使用tesseract命令将OCR应用 ...

  6. 线程安全的集合类、CopyOnWrite机制介绍(转)

    看过并发编程的书,这两种机制都有所了解,但不扎实其实.看到别人的博客描述的很精辟,于是转过来,感谢! 原文链接:https://blog.csdn.net/yen_csdn/article/detai ...

  7. SharePoint Framework 企业向导(五)

    博客地址:http://blog.csdn.net/FoxDave SPFx解决方案是如何被构建出来的--很重要 SPFx为SharePoint开发者在设计.编译和部署SharePoint自定义解 ...

  8. Oracle 一些注意点

    number精度问题 Number(p,s): p和s都是可选的. p指精度(precision),即总位数.默认情况下精度为38.精度的取值范围为1~38. s指小数位(scale),小数点右边的位 ...

  9. 虚拟机提示:无法打开内核设备“\\.\Global\vmx86”: 系统找不到指定的文件

    虚拟机提示:无法打开内核设备“\\.\Global\vmx86”: 系统找不到指定的文件 Win 10 vmware12 无法打开内核设备“\\.\Global\vmx86”: 系统找不到指定的文件. ...

  10. python-tornado和django优缺点

    Django优点: 大和全(重量级框架)自带orm,template,view 需要的功能也可以去找第三方的app注重高效开发全自动化的管理后台(只需要使用起ORM,做简单的定义,就能自动生成数据库结 ...