[Fix] MongoParseError options useCreateIndex, useFindAndModify are not supported

I recently tried running a code in MongoDB, and it said an error, MongoParseError: options useCreateIndex, useFindAndModify are not supported. Here is a snippet of my error:

MongoParseError: options useCreateIndex, useFindAndModify are not supported.

Here are the steps I used to solve the error ‘MongoParseError: options useCreateIndex, useFindAndModify are not supported’.

Why MongoParseError: options useCreateIndex, useFindAndModify are not supported error is seen?

The error, MongoParseError: options useCreateIndex, useFindAndModify are not supported is seen because useNewUrlParser, useUnifiedTopology, useFindAndModify, and useCreateIndex are no longer supported options on Mongoose version 6.0.

Mongoose version 6 always assumes useNewUrlParser, useUnifiedTopology, and useCreateIndex are true, and useFindAndModify is false

A simple solution, please remove these options from your code.

A detailed solution to fix the MongoParseError: options useCreateIndex, useFindAndModify are not supported error is provided below:

How to fix the error MongoParseError: options useCreateIndex, useFindAndModify are not supported?

The error, MongoParseError: options useCreateIndex, useFindAndModify are not supported can be easily solved by following the given steps.

From the Mongoose version 6.0 documentation, you can check which option in your written program is no longer supported. You can find the, docs here.

Mongoose version 6 always assumes useNewUrlParser, useUnifiedTopology, and useCreateIndex are true, and useFindAndModify is false. Please remove these options from your code to fix the error shown. Here is a code snippet to make things more clear.

// No longer necessary:
mongoose.set('useFindAndModify', false);

await mongoose.connect('mongodb://localhost:27017/test', {
  useNewUrlParser: true, // <-- no longer necessary
  useUnifiedTopology: true // <-- no longer necessary
});

If the above solution does not work try to add this code snippet at the end of your code and run it:

mongoose.connect(URI,
    err => {
        if(err) throw err;
        console.log('connected to MongoDB')
    });

This should solve the error MongoParseError: options useCreateIndex, useFindAndModify are not supported.

Conclusion

To fix the error MongoParseError: options useCreateIndex, useFindAndModify are not supported; try to the options which are no longer supported in Mongoose 6.0.

Mongoose version 6 always assumes useNewUrlParser, useUnifiedTopology, and useCreateIndex are true, and useFindAndModify is false