我正试图通过POST请求将一个对象插入mongodb。我发送的对象成功地插入到数据库中,但是我收到了上面提到的错误。
我用于mongo数据库的包:
https://github.com/mongodb/mongo-go-driver
连接字符串
mongodb+srv://user:password@bookcluster.pxcqs.mongodb.net/DBname?retryWrites=true&w=多数`
我设置数据库连接的方式:
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)没有名为"多数"的写问题模式在副本集配置中找到}]
我在Postman 中的请求
我不确定我是否在某个地方错过了某种配置设置——我刚开始使用mongoDB我试着遵循这些教程中的示例:3、4,它们似乎没有提到任何关于"写作关注"one_answers"多数"的内容。还试着查看文档并在谷歌上搜索错误,但没有发现任何帮助。
"mongoURI" : "mongodb+srv://${ db user name }:${ db password }@cluster0.mde0j.mongodb.net/cluster0?retryWrites=true&w=majority "
我得到了相同的错误当我试图通过POST请求将对象插入mongodb时。我发送的对象成功插入数据库,但我收到错误errmsg:"在副本集配置中找不到名为"多数"的写问题模式;。
这是一个简单的错误,您需要删除&w=最后的大部分部分,将通过解决
问题不在于&w=majority
。如果您的连接字符串在.env文件中,那么像下面这样写,不带单引号或双引号,结尾不带分号。
URI=mongodb+srv://user:password@bookcluster.pxcqs.mongodb.net/DBname?retryWrites=true&w=majority
我得到了同样的错误,在连接字符串的末尾有一个额外的空白。就我而言,我去掉了空白,一切都很好。
mongodb+srv://user:password@bookcluster.pxcqs.mongodb.net/DBname?retryWrites=true&w=majority`
删除URI末尾的this(`),因为这将导致">在副本集配置中找不到名为"多数"的写问题模式";错误
尝试删除mongodb.net/{databaseName}?retryWrites=true&w=majority
之后的所有内容,并使其像下面的一样干净整洁
mongoose.connect("mongodb+srv://{nameOfAdmin}:{password}@atlascluster.ux104bi.mongodb.net/{databaseName}", {useNewUrlParser: true, useUnifiedTopology: true})
删除mongodb url 末尾的双引号
在我的例子中,我删除了uri末尾的/,它起了作用。
在我的案例中,我也遇到过同样的错误。我的解决方案是,当我从最后一个(这个-->多数/)中删除正斜杠时,我的问题就解决了。
面向字符串时出错:-mongodb+srv://deepu:IvZ98NCXTOV7ro3M@cluster0.m13scys.mongodb.net/employeDB?retryWrites=true&w=多数/
已解决字符串:-mongodb+srv://deepu:IvZ98NCXTOV7ro3M@cluster0.m13scys.mongodb.net/employeDB?retryWrites=true&w=多数
连接字符串面临相同错误--->
而是使用连接字符串作为-";mongodb+srv://deepu:IvZ98NCXTOV7ro3M@cluster0.m13scys.mongodb.net/(数据库名称)";
删除介于.net/和数据库名称之间的所有内容。
希望这能解决您的疑问!
这可能是因为在.env文件中定义的url末尾使用了空格或分号。所以不要使用任何引号、双引号、分号或空格MONGO_URI=mongodb+srv://Hussain_Siddiqui:Hsrusher8$$@chatapp.aw5bzak.mongodb.net/?retryWrites=true&w=多数仍然有那个警告删除&w=来自url 的多数
从.env文件中删除URI后的分号保存了我.
错误:mongodb+srv://username:password@cluster0.hcyv7.mongodb.net/?retryWrites=true&w=多数;
已解决:mongodb+srv://username:password@cluster0.hcyv7.mongodb.net/?retryWrites=true&w=多数
我通过删除&w=多数。然后我去睡了个午觉。我再次写道&w=uri的多数。它运行得很好。
发生了什么事!我不知道。Justi成功了!
我也面临同样的问题。删除;在连接字符串的末尾为我解决了问题。
看看URL的末尾,可能有一个space
或逗号;
,它们在.env
中是不可接受的字符。
我也有同样的错误,原因是逗号:
DB_URI = mongodb+srv://user:password@cluster0.8fir3uv.mongodb.net/db?retryWrites=true&w=majority,
我在使用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:"多数"},"错误已被删除