62

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?

AlexZeDim
  • 2,626
  • 1
  • 20
  • 50
viiii
  • 657
  • 1
  • 4
  • 8
  • Check the docs for the version of Mongoose you are using, it should list the supported options. – Joe Aug 28 '21 at 01:19
  • if I'm not wrong, the version of mongoose you're talking about is the same as the version of mongoose that I install using npm I mongoose? – viiii Aug 28 '21 at 03:59
  • Does this answer your question? [Option "useFindAndModify" is not supported](https://stackoverflow.com/questions/68915722/option-usefindandmodify-is-not-supported) – NeNaD Sep 14 '21 at 12:11

16 Answers16

114

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.

Joe
  • 19,071
  • 2
  • 15
  • 38
  • 2
    Are these options totally removed or replace with other code?? – viiii Aug 28 '21 at 12:48
  • 1
    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. – Daniele Tentoni Sep 07 '21 at 22:28
  • 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. – Matt Lo Jan 30 '22 at 21:25
24

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.

arex123
  • 241
  • 3
19

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

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')
    });
Seb.code
  • 127
  • 4
9

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;
Jeremy Caney
  • 6,191
  • 35
  • 44
  • 70
viren
  • 109
  • 3
6

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!!!')
})
Mohammed Saeid
  • 113
  • 1
  • 5
5

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!!!')
})
MD SHAYON
  • 7,911
  • 47
  • 35
5

enter image description here

When I commented useNewUrlParser and useCreateIndex it worked for me.

kavi raghul
  • 61
  • 1
  • 3
  • 2
    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](/help/how-to-answer). – Community Dec 21 '21 at 16:38
  • it works for me thank you! – Hicham Mounadi Feb 01 '22 at 08:10
4
Mongoose.connect(
    DB_URL,
    async(err)=>{
        if(err) throw err;
        console.log("conncted to db")
    }
)
bad_coder
  • 8,684
  • 19
  • 37
  • 59
Abhishek P
  • 41
  • 2
  • 2
    Welcome to Stack Overflow. Please edit the post to include an explanation of how the code solves the problem in the question. – bad_coder Aug 29 '21 at 20:35
4
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!!!')
}) 
Om Pranav
  • 41
  • 1
2

//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();
});
jose_ink
  • 21
  • 2
2

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"))
ecm
  • 2,379
  • 4
  • 19
  • 26
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

NcXNaV
  • 1,601
  • 4
  • 12
  • 22
C4LV1N
  • 26
  • 1
  • 2
    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](/help/how-to-ask). – Community Sep 16 '21 at 12:26
0

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 mongodb-community@5.0 :) #HappyCoding

0

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

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

Peter Csala
  • 10,331
  • 15
  • 20
  • 47
  • 1
    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](/help/how-to-answer). – Community Mar 06 '22 at 09:32
-1

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