[Mongoose] How to Fix (node:42424) 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.

Tadashi Shigeoka ·  Tue, December 24, 2019

I’ll introduce how to fix the error (node:42424) 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. that occurs in web applications with Express (Node.js) + Mongoose (MongoDB) configuration.

mongoose | マングース

Solution: Add { useUnifiedTopology: true } Option

(node:42286) DeprecationWarning Error Message

(node:42424) 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.

Sample Code Before Fixing (node:42286) DeprecationWarning

const mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URL);

Sample Code After Fixing (node:42286) DeprecationWarning

const mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URL, { useUnifiedTopology: true });

That’s all from the Gemba.