如何解决 MongooseServerSelectionError: 连接到 MongoDB 时消息大小无效?

问题描述 投票:0回答:1

我在 Node.js 应用程序中尝试使用 Mongoose 连接到 MongoDB 数据库时遇到问题。我收到的错误消息是:

数据库无法连接MongooseServerSelectionError: Invalid 消息大小:1347703880,最大允许:67108864

此错误似乎与 MongoDB 允许的最大消息大小有关。根据错误,正在发送的消息大小为 1347703880 字节,超出了允许的最大大小 67108864 字节 (64 MB)。

这是我使用 Mongoose 连接到 MongoDB 的相关代码:

app.config.js

const MONGO_DB_CONFIG = {
    DB: "mongodb://localhost:27017/ecommerce",
}

module.exports = {
    MONGO_DB_CONFIG,
};

index.js


const express = require("express");
const app = express();
const mongoose = require("mongoose");
const { MONGO_DB_CONFIG } = require("./config/app.config");
const errors = require("./middleware/errors");

mongoose.Promise = global.Promise;
mongoose.connect(MONGO_DB_CONFIG.DB, {
    useNewUrlParser: true,
    useUnifiedTopology: true
}).then(
    () => {
        console.log("Database Connected");
    },
    (error) => {
        console.log("Database can't be connected " + error);
    }
);

// ... (rest of the code)

我已检查 MongoDB 连接字符串 (

mongodb://localhost:27017/ecommerce
) 是否正确,但我不确定是什么原因导致此“消息大小无效”错误。

我也尝试过更新我的 MongoDB 和 Mongoose 版本,但问题仍然存在。以下是我目前使用的版本:

  • MongoDB 版本:6.0.4 MongoDB 版本:6.0.4
  • Mongoose 版本:6.4.4 Mungo 版本:6.4.4

有人可以指导我如何解决此 MongooseServerSelectionError 并成功连接到我的 MongoDB 数据库吗?

javascript node.js mongodb mongoose
1个回答
0
投票

通常我使用以下内容进行 mongodb 连接:

mongoose.connect("mongodb://127.0.0.1:27017/ecommerce", {}, () => console.log("..."));

你能用一个简单的

node index.js
来尝试一下吗?

我认为 mongoose.Promise = global.Promise 是遗留的,还为您的服务器使用 mongodb 端口(process.env.port || 27017)似乎很奇怪

© www.soinside.com 2019 - 2024. All rights reserved.