Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

DeprecationWarning being triggered even with useUnifiedTopology set to true

Hi,

The following message is being triggered whenever I run my node backend.
DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.

My environment:

  • Ubuntu 19.10
  • Mongodb 4.2.5 Community
  • Driver: mongodb 3.5.5
  • Node: 13.12.0
  • Nodemon: 2.0.3
  • Mongodb server: On (locally running) minikube 1.9.2, using kubernetes v1.18.0 on Docker 19.03.2 through kubectl port-foward.
  • I’m running the app through yarn 1.22.4, using nodemon.

Connection:
.env

MONGO_DB_URL=mongodb://localhost:27017
AUTH_DB_NAME=authentication

.js

const MongoClient = mongodb.MongoClient
const url = process.env.MONGO_DB_URL
const mongodbOptions = {
useNewUrlParser: true,
useUnifiedTopology: true
}
const dbClient = new MongoClient(url, mongodbOptions)

const dbName = process.env.AUTH_DB_NAME

const authenticationDb = async () => {
  const dbClient = await client
  if (!dbClient.isConnected()) {
    await dbClient.connect()
  }
  const db = dbClient.db(dbName)
  return db
}

The connection works as well as every database operation, yet I can’t get rid of this warning.

Does anyone have any idea why this is happening?

Thanks in advance.

Please check these links

https://jira.mongodb.org/browse/NODE-2138

Thank you, Ramachandra.

I’ve gone through all the threads, including the linked one (which I actually had already researched beforehand), but as for the code I posted, I am already doing what it was supposed to be done, according to the threads info.
The useUnifiedTopology: true option is being passed to the MongoClient, not to the client.connect.
The used packages, as well as the MongoDB itself, are up to date. Maybe I’m missing something, but I could not find a solution for this warning in those threads. Would you recommend any step further?
I truly appreciate the help and attention.

Hi @Christian_Saboia,
If you are indeed passing useUnifiedTopology: true to your MongoClient constructor, then this message should not display. Can you please update your code sample above if it does not accurately reflect the code you are running? Specifically, I’m seeing this code which seems to refer to a variable which does not exist:

const dbClient = await client
1 Like

Hi Matt,
Actually, there are to files. One is called db.js, which follows:

import mongodb from 'mongodb'
import Grid from 'gridfs-stream'
import multer from 'multer'
import GridFsStorage from 'multer-gridfs-storage'
import path from 'path'
import crypto from 'crypto'

const MongoClient = mongodb.MongoClient
const url = process.env.MONGO_DB_URL
const mongodbOptions = {
  useNewUrlParser: true,
  useUnifiedTopology: true
}

const client = new MongoClient(url, mongodbOptions)

const fileHandler = (bucketName) => (req, file) => {
  return new Promise((resolve, reject) => {
    crypto.randomBytes(16, (err, buf) => {
      if (err) {
        return reject(err)
      }
      const filename = buf.toString('hex') + path.extname(file.originalname)

      // The prop metadata.expires indicates an expiration date in ms for the case where the request fails for any reason.
      // In this (failure) scenario, the expired files can be removed safely.
      // It is the responsability of further request processors (controllers), to remove metadata.expires property
      // from the files it has properly processed or to remove the file (or files), when applicable.
      // An aditional cron job may be created in order to remove orfanned expired files.
      const fileInfo = {
        filename: filename,
        bucketName,
        metadata: {
          originalName: file.originalname,
          encoding: file.encoding,
          mimetype: file.mimetype,
          size: file.size,
          expires: Date.now() + 1000 * 60 * 60
        }
      }
      resolve(fileInfo)
    })
  })
}

const storage = (dbName, bucketName) => {
  const storageUrl = `${url}/${dbName}`
  console.log({ storageUrl })

  return new GridFsStorage({
    url: storageUrl,
    file: fileHandler(bucketName)
  })
}

// accepted options: { fileFilter, limits, preservePath }
const makeUpload = async (dbName, bucketName, options = {}) =>
  multer({ storage: storage(dbName, bucketName), ...options })

const makeGridStream = async (dbName, bucketName) => {
  if (!client.isConnected()) {
    await client.connect()
  }
  const db = client.db(dbName)
  const gfs = Grid(db, mongodb)
  gfs.collection(bucketName)
  return gfs
}

const makeBucket = async (dbName, options) => {
  if (!client.isConnected()) {
    await client.connect()
  }
  const db = client.db(dbName)
  return new mongodb.GridFSBucket(db, options)
}

export { client, makeUpload, makeGridStream, makeBucket }

The second one is called authenticationDB.js:

import { client } from './db'

const dbName = process.env.AUTH_DB_NAME

const authenticationDb = async () => {
  const dbClient = await client
  if (!dbClient.isConnected()) {
    await dbClient.connect()
  }
  const db = dbClient.db(dbName)
  return db
}

export default authenticationDb

This is the actual code, which, despite de warning is working properly.
Thanks in advance!

I’ve managed to find out what was happening. The error was on GridFsStorage creation. I’ve added options property and the message is gone.
Thanks.

1 Like