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();
Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
aditya kumar
  • 2,905
  • 10
  • 39
  • 79
  • 1
    https://stackoverflow.com/questions/51436073/node71307-dep0079-deprecationwarning/51436201#51436201 . Read this . Its just a warning nothing to worry . – Himanshu sharma Aug 22 '18 at 06:12
  • 6
    Follow the link to the duplicated question https://stackoverflow.com/questions/51916630/mongodb-mongoose-collection-find-options-deprecation-warning, in TLDR the answer is to add `mongoose.set('useCreateIndex', true);` before using mongoose.connect – Wiston Coronell Feb 16 '19 at 22:47
  • 3
    you can use ```{ useCreateIndex: true };``` – MD SHAYON Jan 20 '21 at 12:19

1 Answers1

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

Molasses
  • 661
  • 9
  • 12
  • 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 '18 at 06:25
  • so update() and remove() methods are now depricated? – vikrant Mar 12 '19 at 08:16
  • 28
    Use this to connect with DB. mongoose.connect("YOUR DB URL", { useNewUrlParser: true, useCreateIndex: true }); – Sushil Apr 14 '19 at 15:43
  • @Sushil that gives error below in TS with latest mongoose.. `Argument of type '{ useNewUrlParser: true; useCreateIndex: boolean; }' is not assignable to parameter of type 'ConnectionOptions'. Object literal may only specify known properties, and 'useCreateIndex' does not exist in type 'ConnectionOptions'.` – dcsan Jul 03 '19 at 06:44
  • 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 '20 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 03 '20 at 06:20
  • Tried everything and 'still there' –  Nov 21 '20 at 20:06
  • Please look at the linked duplicate for more informative answer - https://stackoverflow.com/questions/51916630/mongodb-mongoose-deprecation-warning?noredirect=1&lq=1 – s7vr Aug 01 '21 at 17:57