node.js 检查mongoDB是否已连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39599063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if mongoDB is connected
提问by Maria Maldini
I have mongoDB in my app.
我的应用程序中有 mongoDB。
I want to check if mongoDB is connected, before I listen to the app.
我想在听应用程序之前检查 mongoDB 是否已连接。
Is it the best way for doing it?
这是最好的方法吗?
This is my server.js file:
这是我的 server.js 文件:
var express = require('express');
var mongoDb = require('./mongoDb');
var app = express();
init();
function init() {
if (mongoDb.isConnected()) {
app.listen(8080, '127.0.0.1');
}
else {
console.log('error');
}
}
isConnectedruns getDbObject.
getDbObjectconnects to mongoDB and returns an object:
connected (true/false), db (dbObject or error).
isConnected运行getDbObject。
getDbObject连接到 mongoDB 并返回一个对象:connected (true/false), db (dbObject or error)。
Then, isConnectedresolve/reject by connected property.
然后,isConnected通过连接属性解决/拒绝。
This is mongoDb.js file:
这是 mongoDb.js 文件:
//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');
// Connection URL. This is where your mongodb server is running.
var url = 'mongodb://localhost:27017/myDb';
var connectingDb; // promise
//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;
init();
module.exports = {
isConnected: isConnected
}
// Use connect method to connect to the Server
function init() {
connectingDb = new Promise(
function (resolve, reject) {
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
reject(err);
}
else {
console.log('Connection established to', url);
//Close connection
//db.close();
resolve(db);
}
});
}
);
}
function getDbObject() {
return connectingDb().then(myDb => {
return {
connected: true,
db: myDb
}
}
)
.catch(err => {
return {
connected: false,
db: err
}
}
)
}
function isConnected() {
return new Promise(
function(resolve, reject) {
var obj = getDbObject();
if (obj.connected == true) {
console.log('success');
resolve(true);
}
else {
console.log('error');
reject(false);
}
}
)
}
Any help appreciated!
任何帮助表示赞赏!
采纳答案by satish chennupati
there are multiple ways depends on how your DB is configured. for a standalone (single) instance. You can use something like this
有多种方式取决于您的数据库的配置方式。对于独立(单个)实例。你可以使用这样的东西
Db.connect(configuration.url(), function(err, db) {
assert.equal(null, err);
if you have a shared environment with config servers and multiple shards you can use
如果您有一个带有配置服务器和多个分片的共享环境,您可以使用
db.serverConfig.isConnected()
回答by Tiago Bértolo
Let clientbe the object returned from MongoClient.connect:
让我们client成为从 MongoClient.connect 返回的对象:
let MongoClient = require('mongodb').MongoClient
let client = await MongoClient.connect(url ...
...
This is how i check my connection status:
这是我检查连接状态的方式:
function isConnected() {
return !!client && !!client.topology && client.topology.isConnected()
}
This works for version 3.1.1 of the driver. Found it here.

