How to change the code so that the nodejs connects to a local database instead of a remote one on Atlas?

The database was previously stored on Atlas and now it needs to be transferred to a local server, since there is no access to it due to company restrictions. I’m trying to change the code of the node js application to connect to db but so far without success.

I created a local mongo database and I can connect to it through mongosh.
But I can’t connect node js application to this database.

What i must to change or add into code to connect to local mongodb?
Every time my web application writes - “there is no connection to the database”.

Working for remote DB on Atlas:
connection to db: db → connection → mongodb.ts

import { MongoClient, MongoClientOptions } from "mongodb"

// @TODO should be typed properly, typing HAVE to be improves
const uri = process.env.MONGODB_URI
const dbName = process.env.MONGODB_DB

let cachedClient: any = null
let cachedDb: any = null

export async function connectToDatabase() {
  // check the cached.
  if (cachedClient && cachedDb) {
    // load from cache
    return {
      client: cachedClient,
      db: cachedDb,

  // set the connection options
  const opts = {
    useNewUrlParser: true,
    useUnifiedTopology: true,

  // Connect to cluster
  let client = new MongoClient(uri!, opts as MongoClientOptions)
  await client.connect()
  let db = client.db(dbName)

  // set cache
  cachedClient = client
  cachedDb = db

  return {
    client: cachedClient,
    db: cachedDb,

I have db ENVs in file .env.local + in Dockerfile


And now I created a local mongo database on the same host (localhost) and node js web application can’t connect to it.

is it still writing to Atlas? Maybe your app is caching the connection? Try restarting the NodeJS app and add a console.log(process.env.MONGODB_URI) to verify its getting the right value.

1 Like

If your

looks like

Then you are still trying to connect to Atlas. You are not trying to connect to

Visit documentation to see how to connect localhost.

Sorry I’m not indicate that this is previous connection settings

And this one is new local connection settings:
1st variant without login and pass
2nd variant with credentials

I run the app and database through docker-compose every time, so there should be any cache.

Is your local mongodb a replica set?

Please share any error messages you get. The message

is one of your application message. It would be nice to see the code that prints it.

Share the command you use to

Share the output of:

ss -tlnp
ps -aef | grep mongo
docker ps

What do you get if you remove ! from uri! in your call new MongoClient.

1 Like

The problem is solved.

In order for the web application to connect to the local database, I added lines to the dockerfile with the installation of mongo for debian 10.

I also indicated everywhere the name of the base that I indicated in docker-compose - “mongo” instead of localhost.

After that, there is a connection and everything works.

It remains to configure so that new data remains after a reboot. But this is another topic.

Thanks to everyone who answered!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.