L
L
Lisio2016-03-23 22:30:18
MongoDB
Lisio, 2016-03-23 22:30:18

How to make MongoClient (mongodb package from node.js) try to connect to database again?

var http = require('http')
  , mongodb = require('mongodb');

var options = {
    {
        uri_decode_auth: true,
        numberOfRetries: 1000,
        db: {
            numberOfRetries: 1000,
            retryMiliSeconds: 5000,
            bufferMaxEntries: 0
        },
        server: {
            auto_reconnect: true,
            socketOptions: {
                connectTimeoutMS: 5000,
            },
            reconnectTries: 1000,
            reconnectInterval: 5000,
        }
};

mongodb.MongoClient.connect('mongodb://username:[email protected]:27017/dbname', options, function(err, db) {
    if (err) {
        console.log('failure');
        return;
    }
    console.log('success');
    http.createServer(function(request, response) {

    }).listen(80);
});

If the database is not running when the script is run, the script prints failure and does nothing else. If you raise the base, then also no reaction. If the database works immediately at startup, then output success and it works correctly.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
un1t, 2016-03-24
@un1t

Well, you yourself indicated

server: {
     auto_reconnect: false,

so it doesn't reconnect.

D
Dmitrii Solovev, 2016-03-24
@dimonnwc3

auto_reconnect: false, maybe you should set it to true first?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question