node.js (节点:63208)弃用警告:不推荐使用 collection.ensureIndex。改用 createIndexes

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51960171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:53:04  来源:igfitidea点击:

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

node.jsmongodbmongoose

提问by aditya kumar

Where is this error coming from? I am not using ensureIndexor createIndexin my Nodejs application anywhere. I am using yarn package manager.

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

Here is my code in index.js

这是我的代码 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();

回答by

The issue is that mongoosestill uses collection.ensureIndex and should be updated by them in the near future. To get rid of the message you can downgrade by using version 5.2.8 in your package.json (and delete any caches, last resort is to uninstall it the install it with npm install [email protected]):

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

"mongoose": "^5.2.8"

“猫鼬”:“^5.2.8”

EDIT:As of this edit, Mongoose is now at v5.4.13. Per their docs, these are the fixes for the deprecation warnings...

编辑:截至本次编辑,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().

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

用 deleteOne() 或 deleteMany() 替换 remove()。

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