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

node.jsMongodbMongoose

node.js Problem Overview


Where is this error coming from? I am not using ensureIndex or createIndex in my Nodejs application anywhere. I am using yarn package manager.

Here is my code in 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 Solutions


Solution 1 - node.js

The issue is that mongoose still 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]):

> "mongoose": "^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.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().

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionaditya kumarView Question on Stackoverflow
Solution 1 - node.jsuser10249973View Answer on Stackoverflow