nodejs mongoDB connection management failed

24 views Asked by At

I think nodejs mongoDB does a good connection close for each API request, but the connection increases explosively and the server often stops. Please refer to the code and connection graph below. Thank you.

async function searchDb(queryObj) {
    return await new Promise(async (resolve, reject) => {
        const client = new MongoClient(
            mongoUri, {
            useNewUrlParser: true,
            useUnifiedTopology: true
        });

        try {
            await client.connect();
            const database = client.db(mongoDB);            // ex) m2n
            const coll = database.collection(collection);   // ex) questions3
            const result = await coll.find(queryObj).toArray();
            resolve(result);

        } catch (error) {
            reject(console.log(`search question error : ${error}`));

        } finally {
            console.log("Closed successfully to server ++");
            await client.close();
        }
    })
}

enter image description here

https://www.mongodb.com/docs/atlas/troubleshoot-connection/#too-many-open-connections-to-your-database-deployment

I found the above site and applied it to the code, but the situation didn't improve, and I asked the cloud.mongodb.com chatbot, but I waited a long time and didn't get a response.

0

There are 0 answers