当前位置: 动力学知识库 > 问答 > 编程问答 >

node.js - Is it possible to use global MongoDb connection state in Mongoose?

问题描述:

In my project, I used plain MongoDb and didn't consider to use Mongoose since I was a beginner and wanted to learn plain MongoDb first without any ORMs.

I've done the learning part and my project grew larger and want to add more Models. Now I'm thinking to use Mongoose without breaking my old mongodb code.

app.listen(process.env.PORT || 4312, function(err) {

if (err) throw err;

var db_uri;

if (app.get('env') === 'development') {

db_uri = test_uri;

} else {

db_uri = uri;

}

db.connect(db_uri, function(err) {

// Global connection state can be accessed like db.state()

if (err) {

debug("Unable to connect to database.", err);

dberror = true;

} else {

debug('Connection Established:', db_uri);

}

});

// If I use Mongoose too, then I've to do something like

mongoose.connect(db_uri, function(err) {

debug('Mongoose connection established');

});

});

Is this the only way to achieve this or can we reuse global mongodb connection state?

分享给朋友:
您可能感兴趣的文章:
随机阅读: