参考:mongoose报错:DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead
mongoose报错:(node:9716)DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead
解决方法:
// 导入第三方模块mongoose
const mongoose = require('mongoose');
// mongoose.set('useCreateIndex', true) //加上这个
// 或连接数据库时添加 useCreateIndex: true
mongoose.connect('mongodb://Jacey:123@localhost:27017/blog2', { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true }) .then(() => console.log('数据库链接成功')) .catch(err => console.log('数据库连接失败:', err));