MongoWriteConcernError: No write concern mode named 'majority;' found in replica set configuration at MessageStream.messageHandler

Viewed 1215

I am using createTask function to insert the document in mongodb atlas , the document is getting saved in atlas but catch part is also working and hence res.send after inserting document is not working.

const createTask=async (req,res)=>{
    try{
      const task = await Task.create(req.body);
    //   res.status(201).json({task});
        res.send('done');
    }
    catch(err){res.status(500).json({msg:err});}
}

When I send the error using catch the error is coming as-

"msg": {
        "code": 79,
        "codeName": "UnknownReplWriteConcern",
        "errInfo": {
            "writeConcern": {
                "w": "majority;",
                "wtimeout": 0,
                "provenance": "clientSupplied"
            }
        },
        "name": "MongoWriteConcernError",
        "result": {
            "n": 1,
            "opTime": {
                "ts": "7021011505814437896",
                "t": 192
            },
            "electionId": "7fffffff00000000000000c0",
            "ok": 1,
            "writeConcernError": {
                "code": 79,
                "codeName": "UnknownReplWriteConcern",
                "errmsg": "No write concern mode named 'majority;' found in replica set configuration",
                "errInfo": {
                    "writeConcern": {
                        "w": "majority;",
                        "wtimeout": 0,
                        "provenance": "clientSupplied"
                    }
                }
            },
            "$clusterTime": {
                "clusterTime": "7021011505814437896",
                "signature": {
                    "hash": "C1HLM9Tmi+sPwFdXIPTEATVpmuA=",
                    "keyId": "6978775665008967681"
                }
            },
            "operationTime": "7021011505814437896"
        }
    }
}
3 Answers

This Error occurs beacuse of your database string, just remove "&w=majority" from the end of setup string.

DB = "mongodb+srv://name:Password@cluster0.f60fb.mongodb.net/mernstack?retryWrites=true&w=majority"

instead write as

DB = "mongodb+srv://name:Password@cluster0.f60fb.mongodb.net/mernstack?retryWrites=true"

hopefully error is gone now.

if still error exists, then check any spaces at the end or rewrite your string. hopefully this will crack.

In your .env file remove &w=majoritytatus(400) from them uri to get the below

mondb_uri=mongodb+srv://name:Password@cluster0.f60fb.mongodb.net/mernstack?retryWrites=true

Remember to re-run whenever you edit .env files

DB = mongodb+srv://name:Password@cluster0.f60fb.mongodb.net/mernstack?retryWrites=true&w=majority

I just write the above query as below and it will work for me(just provided quotes)

DB = "mongodb+srv://name:Password@cluster0.f60fb.mongodb.net/mernstack?retryWrites=true&w=majority"
Related