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

Viewed 12296

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 mentioned above.

The package I'm using for mongo db:

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

Connection string

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

The way I set up the database connection:

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)
    }
}

My object:

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"`
}

Here's how I send the request inside insertBook() (where b is of type Book):

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

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

Full error text:

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

My request in Postman

I'm not sure if I'm missing some sort of a configuration setting somewhere - I just started with mongoDB I tried to follow the example set in these tutorials: 3, 4 and they don't seem to mention anything about the 'write concern' and 'majority'. Also tried looking into the documentation and googling the error but didn't find anything helpful.

7 Answers
"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".

its simple error you need to delete the &w=majority part at the end and it will be solved

The problem isn't with &w=majority. If your connection String is inside .env file, then write it like this one below without single or double quotations and without a semicolon at the end.

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

I got the same error with an extra white space at the end of the connection string. In my case, I removed the white space and all was well.

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

Remove this ( ` ) at end of the URI because this will cause a "No write concern mode named 'majority' found in replica set configuration" error.

in my case i removed / in the end of the uri and it worked.

In my case, i have also faced this same error. My solution for that when i removed forward slash from last(this one --> majority/), my problem solved.

Error facing string:- mongodb+srv://deepu:IvZ98NCXTOV7ro3M@cluster0.m13scys.mongodb.net/employeeDB?retryWrites=true&w=majority/

Solved String:- mongodb+srv://deepu:IvZ98NCXTOV7ro3M@cluster0.m13scys.mongodb.net/employeeDB?retryWrites=true&w=majority

Faced the same error with the connection string--->

instead use the connection string as-"mongodb+srv://deepu:IvZ98NCXTOV7ro3M@cluster0.m13scys.mongodb.net/(databasename)"

Delete everything that is in between .net/ and the database name.

Hope this solves your query!

Related