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

'No write concern mode named 'majority`' found in replica set configuration' error

我正在尝试通过 POST 请求将对象插入 mongodb。我发送的对象已成功插入数据库,但出现上述错误。

我为 mongo db:

使用的包

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

连接字符串

mongodb+srv://user:password@bookcluster.pxcqs.mongodb.net/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)

完整的错误文本:

multiple write errors: [{write errors: []}, {(UnknownReplWriteConcern) No write concern mode named 'majority`' found in replica set configuration}]

My request in Postman

我不确定我是否在某处遗漏了某种配置设置 - 我刚开始使用 mongoDB 我试着按照这些教程中的示例设置:3, 4,他们似乎没有提到任何关于 'write concern' 和 'majority' 的内容。 还尝试查看文档并在谷歌上搜索错误,但没有找到任何有用的信息。

"mongoURI" : "mongodb+srv://${ db user name }:${ db password }@cluster0.mde0j.mongodb.net/cluster0?retryWrites=true&w=majority "

I get the same error with this when I'm trying to insert an object into mongodb via a POST request. The object that I send gets inserted in the db successfully, however I get the error errmsg: "No write concern mode named 'majority' found in replica set configuration".

这个简单的错误你需要把最后的&w=majority部分删掉就可以解决

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

mongodb+srv://user:password@bookcluster.pxcqs.mongodb.net/DBname?retryWrites=true&w=majority`

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

问题不在于 &w=majority。如果你的连接字符串在 .env 文件中,那么像下面这样写,没有单引号或双引号,末尾没有分号。

URI=mongodb+srv://user:password@bookcluster.pxcqs.mongodb.net/DBname?retryWrites=true&w=majority