Skip to content

MongoParseError: Options useCreateIndex and useFindAndModify Not Supported ​

Problem Statement ​

When connecting to MongoDB using Mongoose, you may encounter a MongoParseError with the message that options useCreateIndex and useFindAndModify are not supported. This typically occurs when using Mongoose version 6 or later with connection options that were valid in earlier versions but have been deprecated.

javascript
// This will cause MongoParseError in Mongoose 6+
const URI = process.env.MONGODB_URL;

mongoose.connect(URI, {
   useCreateIndex: true, 
   useFindAndModify: false, 
   useNewUrlParser: true, 
   useUnifiedTopology: true 
}, err => {
   if(err) throw err;
   console.log('Connected to MongoDB!!!')
});

Why This Error Occurs ​

Starting with Mongoose 6, several connection options have been removed because their functionality is now enabled by default:

  • useNewUrlParser and useUnifiedTopology are always true
  • useCreateIndex is always true
  • useFindAndModify is always false

These options are no longer necessary and will cause a parsing error if included in your connection configuration.

Solution ​

Remove the deprecated options from your Mongoose connection configuration. Here's the updated code:

javascript
const URI = process.env.MONGODB_URL;

// Simple connection without deprecated options
mongoose.connect(URI)
  .then(() => console.log('Connected to MongoDB successfully!'))
  .catch(error => console.error('Connection error:', error));

Using async/await Syntax ​

javascript
const connectDB = async () => {
  try {
    await mongoose.connect(process.env.MONGODB_URL);
    console.log('MongoDB connected successfully');
  } catch (error) {
    console.error('Database connection failed:', error.message);
    process.exit(1);
  }
};

connectDB();

With Event Listeners ​

javascript
const URI = process.env.MONGODB_URL;

mongoose.connect(URI);

const connection = mongoose.connection;
connection.once('open', () => {
  console.log('MongoDB database connection established successfully');
});

connection.on('error', (err) => {
  console.error('Connection error:', err);
});

Migration Guide ​

If you're upgrading from Mongoose 5 to Mongoose 6, make the following changes:

javascript
mongoose.connect(URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  useCreateIndex: true,
  useFindAndModify: false
});
javascript
mongoose.connect(URI);

Additional Considerations ​

Strict Query Mode ​

Mongoose 6 enables strict query mode by default. If you need to maintain backward compatibility with existing queries, you can disable it:

javascript
mongoose.set("strictQuery", false);

Callback Syntax Changes ​

Mongoose 6 uses promises instead of callbacks for connections. The callback syntax is no longer supported:

javascript
// No longer works in Mongoose 6
mongoose.connect(URI, (err) => {
  if (err) throw err;
  console.log('Connected');
});

Environment Configuration ​

Ensure your MongoDB connection string in your .env file is properly formatted:

env
MONGODB_URL=mongodb+srv://username:password@cluster0.accdl.mongodb.net/website?retryWrites=true&w=majority

WARNING

Always keep sensitive information like database credentials in environment variables rather than hardcoding them in your application.

Version Compatibility ​

This solution applies to:

  • Mongoose version 6.0.0 and later
  • MongoDB Node.js driver 4.0.0 and later
  • MongoDB server versions 4.0, 4.2, 4.4, 5.0, and later

Conclusion ​

The MongoParseError regarding useCreateIndex and useFindAndModify options is resolved by removing these deprecated configuration options from your Mongoose connection setup. Mongoose 6 simplifies connection configuration by making these options the default behavior, resulting in cleaner, more maintainable code.

For more information, refer to the official Mongoose migration guide.