{"version":"1.0","type":"rich","title":"MongoDb singleton connection in NodeJs","description":"This example shows a singleton pattern for a MongoDB connection in Node.js, ensuring only one database connection is reused across the application. The code uses the `mongodb` library’s `Db`, `Connection`, and `Server` constructors to create a connection to a local database at `127.0.0.1` with `auto_reconnect` enabled. A module-level `connectionInstance` variable caches the open connection; on each call, the exported function either returns the cached instance via a callback or opens a new one if none exists. The approach avoids repeated connection overhead by relying on the `db.open` method.","provider_name":"VizHub AI Studio","provider_url":"https://next.vizhub.com","html":"<iframe src=\"https://next.vizhub.com/viz/gist-e286f618c72ca18adde13a9f92765449?mode=embed\" width=\"960\" height=\"500\" scrolling=\"no\" frameborder=\"1\"></iframe>","width":960,"height":500,"cache_age":3600}