【问题标题】:(node:63208) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead [duplicate](节点:63208)DeprecationWarning:collection.ensureIndex 已弃用。改用 createIndexes [重复]
【发布时间】:2019-01-28 07:41:20
【问题描述】:

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

这是我在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();

【问题讨论】:

标签: node.js mongodb mongoose


【解决方案1】:

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

“猫鼬”:“^5.2.8”

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

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

将 update() 替换为 updateOne()、updateMany() 或 replaceOne()

将 remove() 替换为 deleteOne() 或 deleteMany()。

将 count() 替换为 countDocuments(),除非您想计算整个集合中有多少文档(无过滤器)。在后一种情况下,使用estimatedDocumentCount()。

【讨论】:

  • 我个人认为应该不管它。这是一个弃用警告,你不应该这么费心,因为代码仍然有效并且将在不久的将来升级
  • 所以 update() 和 remove() 方法现在被弃用了吗?
  • 用它来连接数据库。 mongoose.connect("你的数据库 URL", { useNewUrlParser: true, useCreateIndex: true });
  • 我在使用 mongoose 5.9.5 时遇到了同样的问题,我通过修改我的连接解决了这个错误: mongoose.connect(dbUri, {useUnifiedTopology: true, useCreateIndex: true, useNewUrlParser: true});
  • @cr05s19xx 不是真的。因为现在是 2020 年,警告仍然存在。我正在使用 Mongoose 5.10.11
猜你喜欢
  • 2016-02-29
  • 2020-04-20
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
  • 2018-01-03
  • 2014-10-27
  • 2020-09-23
相关资源
最近更新 更多