How to manage MongoDB Connection code and connection pool size

i am facing issue with maintaining connection and connection pool size

exports.connect = function (options = { maxPoolSize: 100 }) {
  return async function (cb) {
    await disconnect();
    let URI;
    if (process.env.IS_REPLICA_SET == "true") {
      URI = `mongodb://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@`
      let sets = [
        process.env.SET_SECONDARY2 + process.env.MONGO_PORT,
        process.env.SET_SECONDARY1 + process.env.MONGO_PORT,
        process.env.SET_PRIMARY + process.env.MONGO_PORT
      URI = `${URI}${sets.toString()}/${process.env.MONGO_DEFAULT_DATABASE}?ssl=true&replicaSet=${process.env.REPLICA_NAME}&authSource=admin&retryWrites=true&w=majority`;
    } else
      URI = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@${process.env.MONGO_CLUSTURE}/${process.env.MONGO_DEFAULT_DATABASE}?retryWrites=true&w=majority`;
    try {
      let db = mongoose.connect(URI, options);
      log.warn("Connecting to MongoDB...");
      mongoose.connection.on('error', error => {
        log.error('Connection lost to MongoDB! ' + error.message);
      // mongoose.set('debug', true);"---------------------------------------");"Connection mode: " + (process.env.IS_REPLICA_SET == "true" ? "Replica set" : "SRV"));"---------------------------------------");
      if (cb) cb(db);"Connection established with MongoDB...");
    } catch (error) {
      log.error('Could not connect to MongoDB! ' + error.message);;

function disconnect() {
  mongoose.disconnect(() => {
    log.warn('Disconnected from MongoDB.');

exports.disconnect = disconnect;

this above code i am using

First of all welcome!

and please describe what is the problem you’re facing with?

Currently we have 5000 Users and the are going to login our site and do some action there
but our mongodb server connection limit goes high more than 4000 connection uses,

we tried with m0 to m40 cluster instance
so need a proper solution by which we get proper response time and minimum connection cpu usages

from the hardware perspective, I am not sure I can give you advice here (other maybe).
but it sounds you need to give more details about your business needs because you have a lot of
data modeling solutions in mongo that can give you this efficiency per request which can reduce resources usage.

if there is a pattern of requests from your client you can adjust your schema to be more effective,
if you want to elaborate please feel free.

I’ll give an example of what I mean: if your client requests documents with a filter on date property and they are always searching in forms whole days (1,2… 10 days) you can potentially use the Bucket pattern and reduce the number of documents processed by Mongo

Currently the issue belong to connection management , connection pooling
how much pool size i need to put for 5000 user login and is my above code is correct for making connection and pool size and is the above code having function to close connection when work done

hey, sorry but i can’t post questions here in the community, so i’m using Stackoverflow, maybe someone can help me with this tricky mongodb issue?