node.js - ( Node :63208) DeprecationWarning: collection. ensureIndex 已弃用。请改用 createIndexes

标签 node.js mongodb mongoose

这个错误来自哪里?我没有在任何地方的 Nodejs 应用程序中使用 ensureIndexcreateIndex。我正在使用 yarn 包管理器。

这是我在 index.js

中的代码
import express from 'express';
import path from 'path';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import Promise from 'bluebird';

dotenv.config();
mongoose.Promise = Promise;
mongoose.connect('mongodb://localhost:27017/bookworm', { useNewUrlParser: true });

const app = express();

最佳答案

问题在于 mongoose仍然使用 collection.ensureIndex 并且应该在不久的将来由他们更新。要摆脱该消息,您可以使用 package.json 中的 5.2.8 版本降级(并删除任何缓存,最后的手段是卸载它并使用 npm install mongoose@5.2.8 安装它):

"mongoose": "^5.2.8"

编辑: 截至本次编辑,Mongoose 现在是 v5.4.13。根据他们的文档,这些是弃用警告的修复...

mongoose.set('useNewUrlParser', true);
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);

Replace update() with updateOne(), updateMany(), or replaceOne()

Replace remove() with deleteOne() or deleteMany().

Replace count() with countDocuments(), unless you want to count how many documents are in the whole collection (no filter). In the latter case, use estimatedDocumentCount().

关于node.js - ( Node :63208) DeprecationWarning: collection. ensureIndex 已弃用。请改用 createIndexes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51960171/

相关文章:

node.js - 如何将 Redis 哈希值转换为 JSON?

php_mongo 扩展已经安装了,但是为什么 rockmongo 仍然没有连接?

javascript - Mongodb 通过 $sample 提供唯一值

javascript - 如何将笔记的多个修订合并为一个版本?

javascript - 如何在保存到 Mongoose (ExpressJS) 之前格式化模型中的数据

javascript - 将解析后的 JSON 读入数组不起作用?

javascript - Sails 1.0,如何从 Action2 调用 customToJSON?

Javascript 将字符串变量添加到响应中

javascript - 用户不是 Express with Passport 中的构造函数

MongoDB : How can I project all field types for a single document?