gpt4 book ai didi

node.js - 如何在 node.js 中重用 mongodb 连接

转载 作者:搜寻专家 更新时间:2023-10-31 22:30:39 24 4
gpt4 key购买 nike

我正在使用 node-mongodb-native 驱动程序和 mongodb 编写网站。

我有一个问题,关于如何打开 mongodb 连接一次,然后在 user.js 中的集合名称 users 和 comment.js 中的集合名称 posts 中使用它

我想在 db.js 中打开数据库连接,然后为用户和帖子集合插入/保存数据

当前代码,我的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);
});
});
});
};

最佳答案

您可以连接一次,然后根据需要多次重复使用:

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 - 如何在 node.js 中重用 mongodb 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17647779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com