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

Issue

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.

Solution

"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

Answered By – Pramod Jaiswal

Answer Checked By – Candace Johnson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.