My MongoDB database connections seem to always expand until they're dropped when the app is restarted (each crash in connections is an app restart). Below on the graph this can be seen. I'm using NodeJS/Express to manage MongoDB as a user database. Why does this occur? Here it's mentioned that Nodejs mongodb connections shouldn't be closed manually.
Tried closing statistical page running concurrent connections, checked code for errors relating to user sessions.
No connections are opened, except for one at the top of the server.js file:
mongoose.connect(DBURI, {
useNewUrlParser: true,
useUnifiedTopology: true
});
And one preforming a leaderboard function:
async function getRanking (callback) {
await mongoClient.connect(uri, function(err, db) {
if (err) throw err;
var dbo = db.db("cluster0");
//Sort the result by name:
var sort = { lastValue: -1 };
dbo.collection("users").find().sort(sort).toArray(function(err, result) {
if (err) {
console.log("Error ranking: " + err);
throw err;
} else {
if (result.length >= 10) {
for (let t = 0; t < 10; t++) {
resultArr += ('|' + result[t].styledUsername.toString() + "!" + result[t].lastValue);
}
} else {
for (let y = 0; y < result.length; y++) {
resultArr += ('|' + result[y].styledUsername.toString() + "!" + result[y].lastValue);
}
}
callback(complete(resultArr));
}
});
});
}
However, at other places in the code queries similar to the one below are used:
User.findById(id, function(err, user) {
done(err, user);
});