一段封装mongodb连接的代码
来源:互联网 发布:udp监听端口 编辑:程序博客网 时间:2024/05/17 00:58
主要目的是对客户端代码屏蔽mongodb connection,避免客户端自行打开和关闭连接
exports.doWithMongo = doWithMongo;var globalConnection;function doWithMongo(callback){ if(globalConnection){ callback(globalConnection); return; } var mongoClient = require('mongodb').MongoClient; var url = "mongodb://192.168.1.111:2222,192.168.1.111:3333,192.168.1.111:4444/planx_graph_r?replicaSet=yilos_mongo_rs&maxPoolSize=5&w=1&journal=true"; mongoClient.connect(url, function (err, db) { globalConnection = db; callback(globalConnection); });}
客户端调用代码:
var mongoHelper = require("./mongoHelper");mongoHelper.doWithMongo(function(db){ db.collection("users").count(function(err, count){ console.log("there are " + count + " documents in the collection: users"); });});mongoHelper.doWithMongo(function(db){ db.collection("test").insert({hello: 'world'}, function(err, objects) { console.log(objects); });});mongoHelper.doWithMongo(function(db){ db.collection("enterprise", {}, function (err, collection) { collection.count(function (err, count) { console.log("there are " + count + " documents in the collection: " + collection.collectionName); }); });});
上面的代码避免客户端自行打开和关闭连接,但是db还是暴露到客户端,改进以后如下:
exports.doWithMongo = operateCollection;var globalConnection;function operateCollection(collectionName, callback){ if(globalConnection){ globalConnection.collection(collectionName, {}, function (err, collection) { callback(collection); }); return; } var mongoClient = require('mongodb').MongoClient; var url = "mongodb://192.168.1.111:2222,192.168.1.111:3333,192.168.1.111:4444/planx_graph_r?replicaSet=yilos_mongo_rs&maxPoolSize=5&w=1&journal=true"; mongoClient.connect(url, function (err, db) { globalConnection = db; db.collection(collectionName, {}, function (err, collection) { callback(collection); }); });}
客户端调用的代码:
var mongoHelper = require("./mongoHelper");mongoHelper.doWithMongo("users", function(collection){ collection.count(function (err, count) { console.log("there are " + count + " documents in the collection: " + collection.collectionName); });});mongoHelper.doWithMongo("test", function(collection){ collection.insert({hello: 'world'}, function(err, objects) { console.log(objects); });});mongoHelper.doWithMongo("enterprise", function(collection){ collection.count(function (err, count) { console.log("there are " + count + " documents in the collection: " + collection.collectionName); });});
这样在客户端,就完全不需要看到connection对象了
0 0
- 一段封装mongodb连接的代码
- 封装测试一段代码的运行时间
- asp连接access数据库的一段代码
- Ubuntu 安装 MongoDB( 含 pymongo 连接封装代码 )
- [分享]一段封装的js表单合法性验证的代码
- 分享一段mysql,mongodb 实现LBS搜索的简单代码
- spring封装mongodb的代码实现
- J2SSH Maverick 一段连接sftp服务器,上传,下载的代码:
- 封装一个简单的PHP连接MongoDB类
- 封装的SOCKET套接字 连接代码
- 一段不可思议的代码
- 一段写晕掉的代码
- 一段好玩的代码
- 一段有趣的代码
- 一段漏洞百出的代码
- 一段弃暗投明的代码
- treeview的一段代码
- 一段分页的代码
- POJ3281--Dining(最大流)
- Web前端技术学习
- 小猿的第一个开源项目:办公自动化软件的通用开发模板 — OAer,简洁为美,轻量交互
- Java学习这七年
- XCode: 兼容ARC和non-ARC
- 一段封装mongodb连接的代码
- 【经济】如何学习经济学之分析
- Matlab贝塞尔函数(Bessel)实现
- 醍醐灌顶
- 解决链接错误:error LNK2001: 无法解析的外部符号 __iob
- 0400000100
- Matlab 高斯消元
- 0400000101
- 0400000102