[Mongoose] (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. の解決方法

Tue, December 24, 2019 - 2 min read

Express (Node.js) + Mongoose (MongoDB) という構成の Web アプリケーションで、 (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. というエラーが発生したときの解決方法をご紹介します。

mongoose | マングース

解決方法 { useUnifiedTopology: true } オプションを付けよう

(node:42286) DeprecationWarning エラーメッセージ

(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.

(node:42286) DeprecationWarning 解決前のサンプルコード

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

(node:42286) DeprecationWarning 解決後のサンプルコード

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

以上、Express + Mongoose で DeprecationWarning 無く開発していきたい、現場からお送りしました。