在连接MongoDB数据库时,去除Node中提示的碍眼东西

在连接MongoDB数据库时,去除Node中提示的碍眼东西:

第一个:

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

第三个:

(node) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes

解决方法:

// 引入mongoose第三方模块
const mongoose = require('mongoose');
// 连接数据库
mongoose.set('useCreateIndex', true) ;//解决第三个
mongoose.connect('mongodb://localhost/blog', {useNewUrlParser: true ,useUnifiedTopology: true})//解决第一个和第二个
	.then(() => console.log('数据库连接成功'))
	.catch(() => console.log('数据库连接失败'));
发布了306 篇原创文章 · 获赞 263 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_44721831/article/details/104163958