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

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:[email protected]/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.



Solution 1:[1]

"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

Solution 2:[2]

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:[email protected]/DBname?retryWrites=true&w=majority

Solution 3:[3]

mongodb+srv://user:[email protected]/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.

Solution 4:[4]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pramod Jaiswal
Solution 2 willko747
Solution 3 ouflak
Solution 4 George R