Mongoose: Suppressing: DeprecationWarning: 'ensureIndex()' is deprecated in Mongoose >= 4.12.0, use 'createIndex()

Created on 4 Oct 2017  路  19Comments  路  Source: Automattic/mongoose

How can we suppress the following warning in Mongoose 4.12.0, when connecting MongoDB with mongoose. This warning message only appears when I upgraded to 4.12.0.

DeprecationWarning: 'ensureIndex()' is deprecated in Mongoose >= 4.12.0, use 'createIndex()' instead

needs clarification

Most helpful comment

All 19 comments

This arrives with a mongoDB 3.4.7 on my machine.

I have the same issue with:

  • Mongoose 4.12.0
  • MongoDB 3.2.16
  • NodeJS 6.11.3

That's strange. Are you calling ensureIndexes() manually? Can you show me your connection code? Mongoose 4.12 should call createIndex() under the hood.

I'm not using ensureIndex() anywhere in my project and I still get this error with v4.12.0.

  mongoose.Promise = global.Promise;
  mongoose.connect(config.MONGO_URI, {
    useMongoClient: true,
    promiseLibrary: global.Promise
  });
  this.connection = mongoose.connection;

I'm not using or calling ensureIndex() anywhere in my code. See below:

var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
var fs = require('fs');
var uri = 'mongodb://user:[email protected]/dbName';
var optionsSSL = { //read in certs};
var connectionOptions = { //specify connection options including OptionsSSL};

mongoose.connect(uri, connectionOptions, function(err)
{
if (err) {console.log(err)};

});

I also feel that this is a strange behavior.

May be this has to do with the MongoDB server version?

I am using MongoDB server version: 3.4.9.

encounter the same problem
mongoose.connect('mongodb://127.0.0.1:27017/text',{ useMongoClient: true},function(err){
if (err) {
console.log('-----------NO------------');
}else{
console.log('-----------OK------------');
}
});
setting session

store: new MongoStore({
mongooseConnection: mongoose.connection,
autoRemove:'native'
})

I've tested things out. MongoStore causes this notice.

I use also MongoStore.

It seems to be an issue with the combination mongoose/MongoStore

Nodejs 6.11.14
mongoose 4.12.0
MongoDB 3.4.9

There's an open pull request in connect-mongo to fix this issue.
https://github.com/jdesboeufs/connect-mongo/pull/269

I can also confirm that the warning message is from the mongo "store" module that I am using for session management, which is connect-mongo.

So replacing connect-mongo with another "store" management module that calls createIndex() should eliminate the warning.

In the alternative, I think there is an open pull request in connect-mongo to fix the issue as mentioned by CivFactions.

Thank you all.

same here people but... this MongoStore thing is in my react project also... i mean, its built in... not my code. so... id be glad to hear solution... please. thanks.

ac1dr3d,
If your mongoStore is using connect-mongo, then you have to wait until the issue is resolve on connect-mongo. It is currently under an open pull request as mentioned by CivFactions.

If you want to replace your mongoStore with another module, check the links below for recommended modules:

1) express-session-mongo: https://www.npmjs.com/package/express-session-mongo
2) connect-mongodb-session: https://www.npmjs.com/package/connect-mongodb-session
3) sessionstore: https://www.npmjs.com/package/sessionstore
4) express-mongoose-store: https://www.npmjs.com/package/express-mongoose-store
5) etc..

Hi,

I use express-session-mongo and I have the warning.........

In fact, express-session + connect-mongodb-session:

session = require('express-session'),
MongoDBStore = require('connect-mongodb-session')(session),

amihailes,
It means that connect-mongodb-session is probably calling ensureIndex() somewhere under the hood. So, it might be worth trying other modules listed above or others that could be found on npm to see which ever works for you.

Working on this!

Thanks @jdesboeufs

Thank you jdesboeufs.

Was this page helpful?
0 / 5 - 0 ratings