如何在node.js中重用mongodb连接 [英] How to reuse mongodb connection in node.js
本文介绍了如何在node.js中重用mongodb连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将mongodb的node-mongodb-native驱动程序用于编写网站.
I'm using node-mongodb-native driver with mongodb to write a website.
我有一个问题,关于如何一次打开mongodb连接,然后在user.js
的集合名称用户和comment.js
I have a question about how to open mongodb connection once, then use it in collection name users in user.js
and collection name posts in comment.js
我想在db.js
中打开数据库连接,然后为用户插入/保存数据并发布帖子
I want to open db connection in db.js
then to insert / save data for users and posts collection
当前代码,我的db.js
var Db = require('mongodb').Db,
Connection = require('mongodb').Connection,
Server = require('mongodb').Server;
module.exports = new Db(
'blog',
new Server('localhost', Connection.DEFAULT_PORT, {auto_reconnect: true})
);
我在user.js
中使用了db.js
var mongodb = require('./db');
function User(user){
this.name = user.name;
this.password = user.password;
this.email = user.email;
};
module.exports = User;
User.prototype.save = function(callback) {//save user information
//document to save in db
var user = {
name: this.name,
password: this.password,
email: this.email
};
mongodb.close();
//open mongodb database
mongodb.open(function(err, db){
if(err){
return callback(err);
}
//read users collection
db.collection('users', function(err, collection){
if(err){
mongodb.close();
return callback(err);
}
//insert data into users collections
collection.insert(user,{safe: true}, function(err, user){
mongodb.close();
callback(err, user);//success return inserted user information
});
});
});
};
和comment.js
var mongodb = require('./db');
function Comment(name, day, title, comment) {
this.name = name;
this.day = day;
this.title = title;
this.comment = comment;
}
module.exports = Comment;
Comment.prototype.save = function(callback) {
var name = this.name,
day = this.day,
title = this.title,
comment = this.comment;
mongodb.open(function (err, db) {
if (err) {
return callback(err);
}
db.collection('posts', function (err, collection) {
if (err) {
mongodb.close();
return callback(err);
}
//depend on name time and title add comment
collection.findAndModify({"name":name,"time.day":day,"title":title}
, [ ['time',-1] ]
, {$push:{"comments":comment}}
, {new: true}
, function (err,comment) {
mongodb.close();
callback(null);
});
});
});
};
推荐答案
您可以连接一次,然后根据需要重复使用多次:
You can connect once, and then reuse it as many times as you want:
var mongodb = require('mongodb');
var events = require('events');
var event = new events.EventEmitter();
var access = new mongodb.Server(host, port, { });
var client = null;
new mongodb.Db('YOUR DATABASE', access, { safe: true, auto_reconnect: true }).open(function (err, c) {
if (!err) {
client = c;
console.log('database connected');
event.emit('connect');
} else {
console.log('database connection error', err);
event.emit('error');
}
});
exports.get = function(fn) {
if(client) {
fn(client);
} else {
event.on('connect', function() {
fn(client);
});
}
};
然后重新使用它:
var db = require('./db');
var items;
db.get(function(client) {
items = new mongodb.Collection(client, 'collection');
});
// then anywhere in your code
db.get(function() {
// items.find({ ...
});
这篇关于如何在node.js中重用mongodb连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文