“在副本集配置中找不到名为‘多数’的写关注模式”错误
我正在尝试通过 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)
完整的错误文本:
多个写入错误:[{write errors: []}, {(UnknownReplWriteConcern) 在副本集配置中找不到名为“majority”的写入关注模式}]
我在邮递员中的请求
我不确定我是否在某处遗漏了某种配置设置 - 我刚开始使用 mongoDB 我试图遵循这些教程中设置的示例:3 , 4他们似乎没有提到任何关于“写关注”和“多数”。还尝试查看文档并搜索错误,但没有发现任何有用的信息。
回答
"mongoURI" : "mongodb+srv://${ db user name }:${ db password }@cluster0.mde0j.mongodb.net/cluster0?retryWrites=true&w=majority "
当我尝试通过 POST 请求将对象插入 mongodb 时,我遇到了同样的错误。我发送的对象成功插入到数据库中,但是我收到错误 errmsg:“在副本集配置中找不到名为 'majority' 的写关注模式”。
它的简单错误你需要在最后删除&w=majority部分,它会被解决