Home > Backend Development > Golang > Why Does My MongoDB Replica Set Return a \'\'No write concern mode named \'majority\' found\' Error, and How Can I Fix It?

Why Does My MongoDB Replica Set Return a \'\'No write concern mode named \'majority\' found\' Error, and How Can I Fix It?

Barbara Streisand
Release: 2024-11-25 01:20:11
Original
980 people have browsed it

Why Does My MongoDB Replica Set Return a

No Write Concern Mode 'Majority' Error in Replica Set Configuration

When attempting to insert an object into MongoDB using the 'mongo-go-driver' package, users may encounter the error: "'No write concern mode named 'majority`' found in replica set configuration'". This error indicates that the replica set does not support the 'majority' write concern mode, which ensures that data is written to a majority of nodes before acknowledging the write operation.

To resolve this issue, it is necessary to update the connection string to remove the write concern parameter:

mongodb+srv://user:[email protected]/DBname
Copy after login

The following lines in the code responsible for setting up the MongoDB connection illustrate where this modification should be made:

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)
    }
}
Copy after login

After updating the connection string, the write operation should succeed without the error.

The above is the detailed content of Why Does My MongoDB Replica Set Return a ''No write concern mode named 'majority' found' Error, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template