MongoParseError: options useCreateIndex, useFindAndModify are not supported

57,485

Solution 1

const URI = process.env.MONGODB_URL;
mongoose.connect(URI, { useUnifiedTopology: true } 
);
const connection = mongoose.connection;
connection.once('open', () => {
    console.log("MongoDB database connection established successfully");
} )

enter image description here

Solution 2

From the Mongoose 6.0 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 3

Same problem was with me but if you remove useCreateIndex, useFindAndModify it will solve the problem just write :

const URI = process.env.MONGODB_URL;
mongoose.connect(URI, {
useNewUrlParser: true, 
useUnifiedTopology: true 
}, err => {
if(err) throw err;
console.log('Connected to MongoDB!!!')
});

It worked for me.

Solution 4

No More Deprecation Warning Options

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.

src --> https://mongoosejs.com/docs/migrating_to_6.html#no-more-deprecation-warning-options

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

Solution 5

I have the same issue. Instaead

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

try this:

mongoose.connect(URI,
    err => {
        if(err) throw err;
        console.log('connected to MongoDB')
    });
Share:
57,485
Author by

viiii

Updated on February 05, 2022

Comments

  • viiii 11 months

    I tried to run it and it said an error like the title. and this is my code:

    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!!!')
    })
    

    I set the MONGODB_URL in .env :

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

    How to fix it?

  • viiii over 1 year
    Are these options totally removed or replace with other code??
  • bad_coder
    bad_coder over 1 year
    Welcome to Stack Overflow. Please edit the post to include an explanation of how the code solves the problem in the question.
  • Daniele Tentoni over 1 year
    This answer from @Joe should be the accepted answer, since it report the documentation. A merge between this and the already accepted answer could be the best answer.
  • Admin
    Admin over 1 year
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
  • econobro
    econobro about 1 year
    You, my friend, are a godsend.
  • Admin
    Admin about 1 year
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
  • Matt Lo 11 months
    This is the winning answer if you're coming from 4.x/5.x ➡️ 6.x. You also need to change the deprecated update, remove, and count methods to the new ones.
  • Hicham Mounadi
    Hicham Mounadi 11 months
    it works for me thank you!
  • Kumar Abhishek 11 months