“在副本集配置中找不到名为“majority”的写入关注模式”错误

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

我正在尝试通过 POST 请求将对象插入到 mongodb 中。我发送的对象已成功插入数据库,但是我收到上面提到的错误。

我用于 mongo db 的包:

https://github.com/mongodb/mongo-go-driver

连接字符串

mongodb+srv://用户:[电子邮件受保护]/DBname?retryWrites=true&w=majority`

我设置数据库连接的方式:

var DbConn *mongo.Client //*sql.DB //*mongo.Client

func SetupDB(conn_str string) {
    var err error
    DbConn, err = mongo.NewClient(options.Client().ApplyURI(conn_str))
    if err != nil {
        log.Fatal(err)
    }
    ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
    err = DbConn.Connect(ctx)
    if err != nil {
        log.Fatal(err)
    }
}

我的目标:

package book

import "go.mongodb.org/mongo-driver/bson/primitive"

type Book struct {
    Id        primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
    Title     string             `json:"title" bson:"title"`
    Author    string             `json:"author" bson:"author"`
    Year      int                `json:"year" bson:"year"`
    ShortDesc string             `json:"shortDesc" bson:"shortDesc"`
    Genre     string             `json:"genre" bson:"genre"`
}

这是我如何在 insertBook() 中发送请求(其中 b 是 Book 类型):

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
    defer cancel()

    result, err := database.DbConn.Database(dbName).Collection(collectionName).InsertOne(ctx, b)

完整错误文本:

多个写入错误:[{写入错误:[]}, {(UnknownReplWriteConcern) 无名为“majority”的写入关注模式 在副本集配置中找到}]

My request in Postman

我不确定我是否在某处缺少某种配置设置 - 我刚刚开始使用 mongoDB 我尝试遵循这些教程中设置的示例:34,他们似乎没有提及任何有关“写入关注”和“多数”的内容。 还尝试查看文档并谷歌搜索错误,但没有找到任何有用的东西。

mongodb go mongodb-query mongodb-atlas
16个回答
23
投票
"mongoURI" : "mongodb+srv://${ db user name }:${ db password }@cluster0.mde0j.mongodb.net/cluster0?retryWrites=true&w=majority "

我也遇到同样的错误 当我尝试通过 POST 请求将对象插入 mongodb 时。我发送的对象成功插入到数据库中,但是我收到错误 errmsg:“在副本集配置中找不到名为“majority”的写入关注模式”。

这是一个简单的错误,你需要删除最后的 &w=majority 部分即可解决


19
投票

问题不在于

&w=majority
。如果您的连接字符串位于 .env 文件内,请像下面这样编写,不带单引号或双引号,并且末尾不带分号。

URI=mongodb+srv://user:[email protected]/DBname?retryWrites=true&w=majority

1
投票

我遇到了同样的错误,连接字符串末尾有一个额外的空格。就我而言,我删除了空白,一切都很好。


1
投票
mongodb+srv://user:[email protected]/DBname?retryWrites=true&w=majority`

删除 URI 末尾的这个 ( ` ),因为这将导致“在副本集配置中找不到名为‘majority’的写入关注模式”错误。


1
投票

尝试删除

mongodb.net/{databaseName}?retryWrites=true&w=majority
之后的所有内容并使其干净整洁,如下所示

mongoose.connect("mongodb+srv://{nameOfAdmin}:{password}@atlascluster.ux104bi.mongodb.net/{databaseName}", {useNewUrlParser: true, useUnifiedTopology: true})

1
投票

删除 mongodb url 末尾的双引号


1
投票

我使用node.js遇到了同样的错误:

 MongoWriteConcernError: No write concern mode named 'majority/' found in replica set configuration"

这是由于catch块之前数据没有写入数据库造成的。

 async function connectToDatabase() {
    try {
          await mongoose.connect(process.env.DATABASE_STRING, {
             useNewUrlParser: true,
             useUnifiedTopology: true,
             writeConcern: { w: 'majority' },
          });
          console.log('Connected to MongoDB');
       } catch (error) {
          console.error('Error connecting to MongoDB:', error);
       }
    }

通过添加“writeConcern: { w: 'majority' }”,该错误已被删除


0
投票

在我的例子中,我删除了 uri 末尾的 / 并且它起作用了。


0
投票

就我而言,我也遇到了同样的错误。我的解决方案是,当我从最后一个(这个 --> 大多数/)中删除正斜杠时,我的问题解决了。

面对字符串时出错:- mongodb+srv://deepu:[电子邮件受保护]/employeeDB?retryWrites=true&w=majority/

已解决的字符串:- mongodb+srv://deepu:[电子邮件受保护]/employeeDB?retryWrites=true&w=majority


0
投票

连接字符串遇到同样的错误--->

而是使用连接字符串作为-"mongodb+srv://deepu:Iv[电子邮件受保护]/(数据库名称)"

删除 .net/ 和数据库名称之间的所有内容。

希望这能解决您的疑问!


0
投票

这可能是因为您在 .env 文件中定义的 url 末尾使用了空格或分号。所以不要使用任何引号、双引号、分号或空格 MONGO_URI = mongodb+srv://Hussain_Siddiqui:[电子邮件受保护]/?retryWrites=true&w=majority 仍然有该警告从 url 中删除 &w=majority


0
投票

从 .env 文件中删除 URI 后面的分号救了我。

错误: mongodb+srv://用户名:passw[电子邮件受保护]/?retryWrites=true&w=majority;

已解决: mongodb+srv://用户名:pass[电子邮件受保护]/?retryWrites=true&w=majority


0
投票

我通过删除 &w=majority 解决了这个问题。然后我就去小睡了。我再次将 &w=majority 写入 uri。效果非常好。

发生了什么事!我不知道。 Jusi它起作用了!


0
投票

我也面临同样的问题。 删除 ;连接字符串的末尾解决了我的问题。


0
投票

查看网址末尾,可能有

space
或逗号
;
,这些是
.env
中不可接受的字符。

我有同样的错误,原因是逗号:

DB_URI = mongodb+srv://user:[email protected]/db?retryWrites=true&w=majority,

0
投票

对于nodeJs,工作就是添加以下内容:到连接字符串

?retryWrites=true&w=majority&maxPoolSize=5

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