Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoParseError: options poolsize, usenewurlparse are not supported

I get the error "MongoParseError: options poolsize, usenewurlparse are not supported" when I run "nodemon server".

Here the code for setting up the mongodb connection:

import app from "./server.js"
import mongodb from "mongodb"
import dotenv from "dotenv"

dotenv.config()
const MongoClient = mongodb.MongoClient

const port = process.env.PORT || 8000

MongoClient.connect(
  process.env.RESTREVIEWS_DB_URI,
  {
    poolSize: 50,
    wtimeout: 2500,
    useNewUrlParse: true,
    }
  )
  .catch(err => {
   
    console.error(err.stack)
    process.exit(1)
  })
  .then(async client => {
   
    app.listen(port, () => {
      console.log(`listening on port ${port}`)
    })
  })
like image 290
Pink Avatar asked Jul 16 '21 22:07

Pink


People also ask

What can I use instead of useCreateIndex?

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: useNewUrlParser , useUnifiedTopology , useFindAndModify , and useCreateIndex are no longer supported options.

Why is useCreateIndex not supported?

The error is because of the new version of the mongoose i.e version 6.0. 6. 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 .

How do you use useUnifiedTopology true?

How to use it. The unified topology is available now behind the useUnifiedTopology feature flag. You can opt in to using it by passing the option to your MongoClient constructor: const client = MongoClient('mongodb://localhost:27017', { useUnifiedTopology: true });

What is useNewUrlParser in mongoose?

useNewUrlParser : The underlying MongoDB driver has deprecated their current connection string parser. Because this is a major change, they added the useNewUrlParser flag to allow users to fall back to the old parser if they find a bug in the new parser.


4 Answers

Some of the MongoClient options have been deprecated.

MongoClient.connect(
    process.env.RESTREVIEWS_DB_URI,
    {
        maxPoolSize: 50, 
        wtimeoutMS: 2500,
        useNewUrlParser: true
    }

).catch(err => {
    console.error(err.stack)
    process.exit(1)
}
like image 111
Dillama Avatar answered Sep 17 '22 12:09

Dillama


The version stopped supporting poolsize,wtimeout and useNewUrlParse.Replace your code with my edit.

import app from "./server.js";
import mongodb from "mongodb"
import dotenv from "dotenv"
dotenv.config()
const MongoClient = mongodb.MongoClient

const port = process.env.PORT || 8000

MongoClient.connect(
    process.env.RESTREVIEWS_DB_URI,
    {
        maxPoolSize:50,
        wtimeoutMS:2500,
        useNewUrlParser:true
    }
)
.catch(err => {
    console.error(err.stack)
    process.exit(1)
})
.then(async client => {
    app.listen(port, () => {
        console.log('listening on port '+port)
    })
})
like image 30
Jinoy Varghese Avatar answered Sep 17 '22 12:09

Jinoy Varghese


const mongoose = require('mongoose');
require('dotenv').config();
const user = process.env.mongoUser;
const pass = process.env.mongoPass;
const url = `mongodb+srv://${user}:${pass}@cluster0.asqnt.mongodb.net/MyDB`;

mongoose.connect(url, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true
})
.then(console.log('connecting'))
.catch(err => console.log(`error: ${err}`))
like image 44
Umar Avatar answered Sep 18 '22 12:09

Umar


After changes in MongoDB-native diver to 4.x, you need to just change MongoClientOptions interface:

you have this:

MongoClient.connect(
  process.env.RESTREVIEWS_DB_URI,
  {
    poolSize: 50, // maxPoolSize
    wtimeout: 2500, // wtimeoutMS
    useNewUrlParse: true, // feel free to remove, no longer used by the driver.
    }
  )

you need:

MongoClient.connect(
  process.env.RESTREVIEWS_DB_URI,
  {
    maxPoolSize: 50,
    wtimeoutMS: 2500,
  }
)
like image 28
Denys Pugachov Avatar answered Sep 20 '22 12:09

Denys Pugachov