typeorm: Can't Connect To MongoDB Atlas Cluster

Issue type:

[ ] question [X] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [X] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[X] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

I have a valid connection string to a Mongo Atlas DB that I am trying to use with TypeORM. I know the connection string works because I have used it from Python and from the Mongo shell. But it doesn’t work from typeorm. I have tried both the mongodb:// and mongo+srv synax. Not sure how to debug or figure this one out. Here is the error:

(node:19257) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
{ MongoNetworkError: connection 5 to cluster0-XXXX.mongodb.net:27017 closed
    at Socket.<anonymous> (/home/administrator/Source/Repos/emoon-relayer/node_modules/mongodb-core/lib/connection/connection.js:276:9)
    at Object.onceWrapper (events.js:273:13)
    at Socket.emit (events.js:182:13)
    at TCP._handle.close (net.js:611:12)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 20 (6 by maintainers)

Commits related to this issue

Most upvoted comments

After few tries I have two valid configurations for typeorm and mongo atlas:

const connection = await createConnection({
            type: "mongodb",
            useNewUrlParser: true,
            url: "mongodb://admin:password@testcluster0-shard-00-00-1cmyo.mongodb.net:27017,testcluster0-shard-00-01-1cmyo.mongodb.net:27017,testcluster0-shard-00-02-1cmyo.mongodb.net:27017/test?ssl=true&replicaSet=TestCluster0-shard-0&authSource=admin&retryWrites=true",
            ssl: true,
            authSource: "admin",
            replicaSet: "TestCluster0-shard-0"
        });

and second one

const connection = await createConnection({
            type: "mongodb",
            url: "mongodb+srv://admin:admin@testcluster0-1cmyo.mongodb.net/test?retryWrites=true",
            ssl: true,
            authSource: "admin",
            replicaSet: "TestCluster0-shard-0"
        });

But I think @pointtoken is right. Calling mongodb connect method with undefined option properties overwrites settings from url.

So the below worked for me. I am using Nestjs

{
  "type": "mongodb",
  "url": "mongodb+srv://<user>:<password>@cluster0.somethingsomething.mongodb.net/<dbname>?retryWrites=true&w=majority",
  "useNewUrlParser": true,
  "synchronize": true,
  "logging": true,
  "useUnifiedTopology": true,
  "entities": ["dist/**/*.entity{.ts,.js}"]
}

I am still getting this problem with typeorm 0.2.29. None of the solutions posted above work right now

EDIT: I found a working but undocumentend solution, if you use one of your cluster shard addresses instead of the general address mongo atlas gives you, the connection works

So instead of mongodb+srv://<user>:<password>@cluster0.somethingsomething.mongodb.net/<dbname> use something like (you’ll find three valid for your conf in your mongoatlas page): mongodb+srv://<user>:<password>@cluster0-shard-00-00.somethingsomething.mongodb.net/<dbname>

hope it helps

I know it’s a closed case… but since I’m using dotenv and MongoDB Atlas this is what I found :

.env

TYPEORM_CONNECTION = mongodb
TYPEORM_URL = mongodb+srv://admin:password@cluster-XXXXX-pSDig.mongodb.net/graphql-ts?retryWrites=true&w=majority
TYPEORM_DATABASE = graphql-ts

TYPEORM_ENTITIES = src/entity/*.ts
TYPEORM_MIGRATIONS = src/migration/**/*.ts
TYPEORM_ENTITIES_DIR = src/entity
TYPEORM_SUBSCRIBERS_DIR = src/subscriber
TYPEORM_MIGRATIONS_DIR = src/migration

TYPEORM_DRIVER_EXTRA = '{"useUnifiedTopology":true}'

@PierBusDev is correct. MongoDB Atlas support is currently broken unless you specify the exact shard you want to connect to. Wasted a few hours on this 😕 This issue should be re-opened with a fix or with some documentation.

Updating this ticket with a code block that got mine to work, using the free cluster.

  const connection = await createConnection({
    type: 'mongodb',
    url: 'mongodb+srv://<user>:<pass>@test-cluster.cgl5c.mongodb.net/<db name>',
    w: 'majority',
    ssl: true,
    authSource: 'admin',
    entities: ['dist/**/*.entity.js'],
  });

Hello,

I have the same problem with a free Mongo Atlas cluster.

type: 'mongodb',
      url:
        'mongodb+srv://***:***@test-tqajp.gcp.mongodb.net/test?retryWrites=true&w=majority',
      entities: [join(__dirname, '**', '*.type.{ts,js}')],
      synchronize: true,
      useNewUrlParser: false,
      logging: true,

[TypeOrmModule] Unable to connect to the database. Retrying (1)... +2006ms

I’m using nestjs, but with mongoose all works fine. But i’d like using typeorm because i need multiple connections with other kind of databases. I don’t think it’s a problem of @nestjs/typeorm package.