Mongoose/MongoDB in Node.JS stops returning records after a while

Viewed 806

I have a script running which finds data in a database I have. For some reason after a short period of time, it stops finding any data but does not output any errors to the screen or through node.js. Here is the following setup I have for mongoose. I have a database at MLAB btw!

var connectionOptions = {
    server: {
        auto_reconnect: true,
        poolSize: 16,
        socketOptions: {
            keepAlive: 120,
            connectTimeoutMS: 5 * 60 * 60 * 1000,
            socketTimeoutMS: 5 * 60 * 1000
        }
    },
    replset: {
        poolSize: 16,
        socketOptions: {
            connectTimeoutMS: 300000, // 5 minutes
            keepAlive: 120
        },
        ha: true, // Make sure the high availability checks are on
        haInterval: 5000, // Run every 10 seconds
    }
};

//mongoose.set('debug', true);
mongoose.connect('mongodb://XXXXXXXXXXXX@XXXXXX.mlab.com:57328,XXXXXXXX.mlab.com:57328/XXXXXXXXX?replicaSet=rs-XXXXXX', connectionOptions, function (err) {
    if (err) console.log(err);
});

mongoose.connection.on('connecting', function () {
    console.log('Connecting to MongoDB...');
});

mongoose.connection.on('connected', function () {
    console.log('MongoDB connected!');
});

mongoose.connection.on('open', function () {
    console.log('MongoDB connection opened!');
});

mongoose.connection.on('error', function (err) {
    console.log(err);
    mongoose.disconnect();
});

mongoose.connection.on('disconnected', function () {
    console.log('MongoDB disconnected!');
    mongoose.connect('mongodb://XXXXXXXXXXXX@XXXXXX.mlab.com:57328,XXXXXXXX.mlab.com:57328/XXXXXXXXX?replicaSet=rs-XXXXXX', connectionOptions, function (err) {
        if (err) console.log(err);
    });
});

mongoose.connection.on('reconnected', function () {
    console.log('MongoDB reconnected!');
});

mongoose.connection.on('close', function () {
    console.log.error('MongoDB closed');
});

mongoose.connection.on('ha', function(type, data) {
    console.log('replset ha ' + type);
});

mongoose.connection.on('timeout', function () {
    console.log.error('MongoDB timeout');
});

I have seen a lot of configurations and I've tried a ton of different things but nothing seems to work.

I would really appreciate the help, as I've been researching around for quite a bit to no luck.

And to recap again, I have a script running querying records from a database, and after a certain time, the script will keep running but seem to not find any new records and stop responding to any new data.

1 Answers

Not sure about your configurations there but you can try mine:

  mongoose.connect(
  process.env.DB_CONNECT,
  {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true,
  },
  () => {
    console.log("Connected To MongoDB");
  }
);
Related