114

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();
3

1 Answer 1

257

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 mongoose@5.2.8):

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

8
  • 12
    I personally think it should be left alone. It is a deprecation warning and you shouldn't bother so much because the code still functions and will be upgraded in the near future
    – cross19xx
    Oct 20, 2018 at 6:25
  • so update() and remove() methods are now depricated?
    – vikrant
    Mar 12, 2019 at 8:16
  • 28
    Use this to connect with DB. mongoose.connect("YOUR DB URL", { useNewUrlParser: true, useCreateIndex: true });
    – Sushil
    Apr 14, 2019 at 15:43
  • 9
    I faced the same issue while using mongoose 5.9.5 i resolved the error by modifying my connection to: mongoose.connect(dbUri, {useUnifiedTopology: true, useCreateIndex: true, useNewUrlParser: true});
    – Hamfri
    Mar 19, 2020 at 18:47
  • 5
    @cr05s19xx Not really. Because it's 2020 now and the warning is still there. And I am using Mongoose 5.10.11
    – Sandun
    Nov 3, 2020 at 6:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.