title: Error connecting to MongoDB with Loopback 4
body: <p>I'm having trouble connecting to MongoDB from Loopback 4.  We have lots of services that connect to MongoDB from Loopback 3, but this is the first attempt at using Loopback 4.  The error I'm getting is:</p>
  <pre><code>(node:1) [MONGODB DRIVER] Warning: Top-level use of w, wtimeout, j, and fsync is deprecated. Use writeConcern instead.
  (Use `node --trace-warnings ...` to show where the warning was created)
  Server is running at https://[::1]:8000
  Connection fails: MongoServerSelectionError: unsupported certificate purpose
  It will be retried for the next request.
  /usr/src/app/node_modules/mongodb/lib/utils.js:698
            throw error;
            ^
  
  MongoServerSelectionError: unsupported certificate purpose
      at Timeout._onTimeout (/usr/src/app/node_modules/mongodb/lib/core/sdam/topology.js:438:30)
      at listOnTimeout (internal/timers.js:549:17)
      at processTimers (internal/timers.js:492:7)
  Emitted 'error' event on DbDataSource instance at:
      at DbDataSource.postInit (/usr/src/app/node_modules/loopback-datasource-juggler/lib/datasource.js:502:16)
      at onError (/usr/src/app/node_modules/loopback-connector-mongodb/lib/mongodb.js:316:21)
      at /usr/src/app/node_modules/loopback-connector-mongodb/lib/mongodb.js:324:9
      at /usr/src/app/node_modules/mongodb/lib/utils.js:695:9
      at /usr/src/app/node_modules/mongodb/lib/mongo_client.js:260:23
      at connectCallback (/usr/src/app/node_modules/mongodb/lib/operations/connect.js:365:5)
      at /usr/src/app/node_modules/mongodb/lib/operations/connect.js:552:14
      at Object.connectHandler [as callback] (/usr/src/app/node_modules/mongodb/lib/core/sdam/topology.js:286:11)
      at Timeout._onTimeout (/usr/src/app/node_modules/mongodb/lib/core/sdam/topology.js:443:25)
      at listOnTimeout (internal/timers.js:549:17)
      at processTimers (internal/timers.js:492:7) {
    reason: TopologyDescription {
      type: 'ReplicaSetNoPrimary',
      setName: null,
      maxSetVersion: null,
      maxElectionId: null,
      servers: Map(3) {
        'replica01:27017' =&gt; [ServerDescription],
        'replica02:27017' =&gt; [ServerDescription],
        'replica03:27017' =&gt; [ServerDescription]
      },
      stale: false,
      compatible: true,
      compatibilityError: null,
      logicalSessionTimeoutMinutes: null,
      heartbeatFrequencyMS: 10000,
      localThresholdMS: 15,
      commonWireVersion: null
    }
  }
  </code></pre>
  <p>Here is the code I'm using to connect (this was generated with the <code>lb4 datasources</code> command):</p>
  <pre><code>import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core';
  import {juggler} from '@loopback/repository';
  
  const config = {
    name: 'db',
    connector: 'mongodb',
    url: 'mongodb://replica01:27017,replica02:27017,replica03:27017/mydb?replicaSet=rs0&amp;ssl=true&amp;tlsCAFile=/etc/mongo-cert/mongoCA.pem',
    host: 'mongo',
    port: 27017,
    user: '',
    password: '',
    database: 'mydb',
    useNewUrlParser: true,
    ssl: true,
    sslCA: '/etc/mongo-cert/mongoCA.pem',
    debug: true
  };
  
  // Observe application's life cycle to disconnect the datasource when
  // application is stopped. This allows the application to be shut down
  // gracefully. The `stop()` method is inherited from `juggler.DataSource`.
  // Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html
  @lifeCycleObserver('datasource')
  export class DbDataSource extends juggler.DataSource
    implements LifeCycleObserver {
    static dataSourceName = 'db';
    static readonly defaultConfig = config;
  
    constructor(
      @inject('datasources.config.db', {optional: true})
      dsConfig: object = config,
    ) {
      super(dsConfig);
    }
  }
  </code></pre>
  <p>Initially, I was using version 5.5.0 of <code>loopback-connector-mongodb</code>, but tried upgrading to 6.0.1, with no change.</p>
  <p>If anyone has any thoughts on how to fix this error, I would greatly appreciate it.</p>