1

I am trying to understand between using connection pooling vs using singleton connection for connecting to mongodb.

Which one is the better approach? What is difference in caching the connections in connection pooling vs instantiating a class once in singleton and using same elsewhere?

1 Answers1

0

I suggest you to use a more high level api for interacting with Mongo. A well know one in node environment is mongoose. This library is widely used and has built in support for connection pooling, see doc. Using a high level module like mongoose can help you to boost productivity and avoid common pitfall like security bad practices etc.

Eduard Hasanaj
  • 865
  • 4
  • 10
  • Thanks Eduard Hasanah. I am currently using mongoose and connection pooling, but I want to know if there is any advantage while using this connection pooling other than using singleton class – Vysakh_Prem Oct 08 '20 at 10:29
  • @Vysakh_Prem What do you mean with singleton class? You need to create a module where you instantiate an instance of mongoose and connect to database. After this you can import this module and use the same client where you want. Please if my question is helpful accept it. – Eduard Hasanaj Oct 08 '20 at 12:43
  • Thanks @Eduard Hasanaj. I found an example on using singleton classes here.https://stackoverflow.com/questions/24547357/setting-up-singleton-connection-with-node-js-and-mongo. Right now using mongodb connection pooling – Vysakh_Prem Oct 14 '20 at 09:15