Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

376
Visualizações
Is it neccessary to close mongodb connection after manipulating or fetching data?

I have this below code it works correctly but should I close the connection after returning the data? If yes so how to close the connection?

// project.db.js

 const { MongoClient, ObjectId } = require("mongodb");
 const { mongo_uri, dbName } = require("../config/constants");

// connect method
 function connection() {
    return MongoClient.connect(mongo_uri);
 }
// async function which read data
  exports.allProject_tasks = async (space_id) => {
        try {
        const result = await connection();
        const db = await result.db(dbName);
        const payload = await db
       .collection("tasks")
         .find({
            user_space_id: ObjectId(space_id),
          })
     .toArray();
     return payload;
 } catch {
    return 400;
}
  };
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It is always best to close the connection when the job is done from app side to release the allocated connection resources on database server side( mongoDB allocated ~1MB per connection meaning that if you open 1000 connections your server will allocate ~1GB only for connections) , and if you leave the resource to be released by the connection timeouts this could lead to issues when server is under high load if the timeout is higher.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda