NodeJS: mongoose Deprecation Warning: collection.ensureIndex is deprecated. Use createIndexes instead


Example Warning:

(node:73172) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
(Use `node --trace-deprecation ...` to show where the warning was created)

How to fix this problem ?

Add useCreateIndex:true to your options

mongoose.connect('mongodb://127.0.0.1:27017/test', { 
            useNewUrlParser: true,
            useUnifiedTopology: true,
            useCreateIndex:true 
        })

Leave a Reply