mongodb 蒙戈解析错误:不支持选项poolsize,usenewurlparse

mmvthczy  于 2023-03-29  发布在  Go
关注(0)|答案(8)|浏览(188)

我得到错误“MongoParseError:选项poolsize,usenewurlparse是不支持的”当我运行“nodemon服务器”。
下面是设置mongodb连接的代码:

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}`)
    })
  })
swvgeqrz

swvgeqrz1#

一些MongoClient选项已被弃用。

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

).catch(err => {
    console.error(err.stack)
    process.exit(1)
}
axkjgtzd

axkjgtzd2#

该版本停止支持poolsize,wtimeout和useNewUrlParse。用我的编辑替换你的代码。

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)
    })
})
c86crjj0

c86crjj03#

在MongoDB-native diver更改为4.x之后,您只需更改MongoClientOptions接口:

你有这个:

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

您需要:

MongoClient.connect(
  process.env.RESTREVIEWS_DB_URI,
  {
    maxPoolSize: 50,
    wtimeoutMS: 2500,
  }
)
zi8p0yeb

zi8p0yeb4#

我也遇到了同样的问题,这就是我如何管理的,在你的代码中,替换

{
    poolSize: 50,
    wtimeout: 2500,
    useNewUrlParse: true,
    }

{
  
  maxPoolSize: 50,
  wtimeoutMS: 2500,
  useNewUrlParser: true,
}
yx2lnoni

yx2lnoni5#

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}`))
uqjltbpv

uqjltbpv6#

我刚才正试着启动服务器,这时我注意到:

{
   useNewUrlParser: true,
   useUnifieldTopology: true,
   useFindAndModify: false
}

它们已经被弃用了。看看这个出版物,有效的是来自@Jinoy Varghese的那个。我把我的蒙古配置代码留给你来连接数据库。

const mongoose = require('mongoose')
require('dotenv').config({ path: '.env' })

const connectDB = async () => {
    try {
        await mongoose.connect(process.env.DB_MONGO, {
            maxPoolSize: 50,
            wtimeoutMS: 2500,
            useNewUrlParser: true
        })
        console.log('DB connected ')
    } catch (err) {
        console.log(err)
        process.exit(1)
    }
}
module.exports = connectDB
wwtsj6pe

wwtsj6pe7#

将此配置用于mongoose

const express = require('express');
const mongoose = require('mongoose');

mongoose.Promise = global.Promise;
mongoose.set('useNewUrlParser', true);
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('useUnifiedTopology', true);
mongoose.connect(your mongodb URI).then(()=> {
    app.listen(PORT, () => {
        console.log(`Listening on port` + PORT);
    })
}).catch((e) => {console.log(e)})

而且app.listen不是一个异步函数,你不需要使用async

roqulrg3

roqulrg38#

poolSize、wtimeout和useNewUrlParse已弃用。请替换为maxPoolSize、wtimeoutMS和useNewUrlParser。没有它,您的代码将无法工作。

相关问题