MongoParseError: options useCreateIndex, useFindAndModify are not supported

Mongodb

Mongodb Problem Overview


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?

Mongodb Solutions


Solution 1 - Mongodb

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 2 - Mongodb

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 3 - Mongodb

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 4 - Mongodb

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

Solution 5 - Mongodb

The error is because of the new version of the mongoose i.e version 6.0.6.

As the documentation says:

> 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.

Also, there are some major changes in the new version.

For more info visit https://mongoosejs.com/docs/migrating_to_6.html#no-more-deprecation-warning-options

import mongoose from 'mongoose';
    
    const db = process.env.MONGOURI;
    
    const connectDB = async () => {
      try {
        console.log(db);
        await mongoose.connect(`${db}`, {
          useNewUrlParser: true,
          useUnifiedTopology: true,
        });
        console.log('MongoDB connected');
      } catch (error) {
        console.log(error.message);
        process.exit(1);
      }
    };
    
    
    export default connectDB;

Solution 6 - Mongodb

remove usecreateindex, usefindandmodify options

const URI = process.env.MONGODB_URL;

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

Solution 7 - Mongodb

What version of Mongoose are you using? The useCreateIndex option has been deprecated for a while and removed as of the Mongoose 6 release per No More Deprecation Warning Options:

const URI = process.env.MONGODB_URL;

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

Solution 8 - Mongodb

enter image description here

When I commented useNewUrlParser and useCreateIndex it worked for me.

Solution 9 - Mongodb

Mongoose.connect(
    DB_URL,
    async(err)=>{
        if(err) throw err;
        console.log("conncted to db")
    }
)

Solution 10 - Mongodb

const URI = process.env.MONGODB_URL;

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

Solution 11 - Mongodb

//this is working for me at date/version (08-2021

const mongoose = require('mongoose');
var url = "mongodb+srv://username:<password>@cluster0.accdl.mongodb.net/website? 
retryWrites=true&w=majority";
mongoose.connect(url, function(err, db) {
    if (err) throw err;
        console.log("Database created!");
        db.close();
});

Solution 12 - Mongodb

Use this to check your database connection:

const mongoose = require("mongoose");
const url = ... /* path of your db */;

//to connect or create our database
mongoose.connect(url, { useUnifiedTopology : true, useNewUrlParser : true , }).then(() => {
   console.log("Connection successfull");
}).catch((e) => console.log("No connection"))

Solution 13 - Mongodb

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 14 - Mongodb

I faced the same error. Just remove the "useCreateIndex: true" and it will work but make sure the mongoDB service is running on your local machine in the first place ~ brew services start [email protected] :) #HappyCoding

Solution 15 - Mongodb

useNewUrlParser, useUnifiedTopology, useFindAndModify, and useCreateIndex are no longer supported options.

basically, just remove that object and you'll be fine:)

Solution 16 - Mongodb

October 27th 2021 Without a try catch it won't work. I am just posting this to keep everyone up to date with mongo connections.

    const uri = process.env.ATLAS_CONNECTION;
    mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: 
    true });

    const connection = mongoose.connection;


    try{
    connection.once('open', () => {
        console.log("MongoDB database connection established 
     successfully");
    })
    } catch(e) {
    console.log(e);
    }

   function close_connection() {
    connection.close();
   }

Connection Established

Categories

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionviiiiView Question on Stackoverflow
Solution 1 - MongodbJoeView Answer on Stackoverflow
Solution 2 - Mongodbarex123View Answer on Stackoverflow
Solution 3 - MongodbMd. Jahid HossainView Answer on Stackoverflow
Solution 4 - MongodbSeb.codeView Answer on Stackoverflow
Solution 5 - MongodbvirenView Answer on Stackoverflow
Solution 6 - MongodbMohammed SaeidView Answer on Stackoverflow
Solution 7 - MongodbMD SHAYONView Answer on Stackoverflow
Solution 8 - Mongodbkavi raghulView Answer on Stackoverflow
Solution 9 - MongodbAbhishek PView Answer on Stackoverflow
Solution 10 - MongodbOm PranavView Answer on Stackoverflow
Solution 11 - Mongodbjose_inkView Answer on Stackoverflow
Solution 12 - MongodbKrushna BarkuleView Answer on Stackoverflow
Solution 13 - MongodbC4LV1NView Answer on Stackoverflow
Solution 14 - MongodbPiyush ChandraView Answer on Stackoverflow
Solution 15 - MongodbRicardo alvarezView Answer on Stackoverflow
Solution 16 - MongodbJessica WoodsView Answer on Stackoverflow