Mongoose + MongoDB - Fix for MongoParseError: options usecreateindex, usefindandmodify are not supported
I updated a Node.js project this morning to use the latest version of Mongoose (7.0.3) to connect to MongoDB and received the following error on startup:
C:\Projects\node-mongo-registration-login-api\node_modules\mongodb\lib\connection_string.js:286
throw new error_1.MongoParseError(`${optionWord} ${Array.from(unsupportedOptions).join(', ')} ${isOrAre} not supported`);
^
MongoParseError: options usecreateindex, usefindandmodify are not supported
at parseOptions (C:\Projects\node-mongo-registration-login-api\node_modules\mongodb\lib\connection_string.js:286:15)
at new MongoClient (C:\Projects\node-mongo-registration-login-api\node_modules\mongodb\lib\mongo_client.js:46:63)
at _createMongoClient (C:\Projects\node-mongo-registration-login-api\node_modules\mongoose\lib\connection.js:851:14)
at NativeConnection.openUri (C:\Projects\node-mongo-registration-login-api\node_modules\mongoose\lib\connection.js:705:29)
at Mongoose.connect (C:\Projects\node-mongo-registration-login-api\node_modules\mongoose\lib\index.js:406:15)
at Object.<anonymous> (C:\Projects\node-mongo-registration-login-api\_helpers\db.js:4:10)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12) {
[Symbol(errorLabels)]: Set(0) {}
}
This is the code I was using to connect to the database:
const config = require('config.json');
const mongoose = require('mongoose');
const connectionOptions = { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false };
mongoose.connect(process.env.MONGODB_URI || config.connectionString, connectionOptions);
...
Cause of the Mongoose error
The error was caused by the connectionOptions
being passed to the mongoose.connect()
method - { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false }
.
Since Mongoose v6 these are the default values for the connection options and should be removed. The below message is from the Mongoose docs:
useNewUrlParser
, useUnifiedTopology
, useFindAndModify
, and useCreateIndex
are no longer supported options. Mongoose 6 always behaves as if useNewUrlParser
, useUnifiedTopology
, and useCreateIndex
are true
, and useFindAndModify
is false
. Please remove these options from your code.
Solution
To fix the error I just had to remove the options when connecting to MongoDB with mongoose.connect()
, below is the updated code:
const config = require('config.json');
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI || config.connectionString);
...
Need Some MongoDB Help?
Search fiverr for freelance MongoDB developers.
Follow me for updates
When I'm not coding...
Me and Tina are on a motorcycle adventure around Australia.
Come along for the ride!