The meaning of ?retryWrites=true&w=majority when you connect Node.js app to Cluster on MongoDB Atlas

Viewed 2746

When we connect a Node.js application to a cluster on the mongoDB Atlas, we need to add the following URI instead of adding "mongodb://localhost:27017/<dbname>" as you can see at the step 2 in the image.

"mongodb+srv://<username>:<password>@cluster1.tv7cp.mongodb.net/<dbname>?retryWrites=true&w=majority"

Now, what exactly the meaning of ?retryWrites=true&w=majority in it after <dbname>? Why do we need to add it?

I tried and ran the app with mongoose without ?retryWrites=true&w=majority, and it worked fine.

enter image description here

1 Answers

Retryable writes allow MongoDB drivers to automatically retry certain write operations a single time if they encounter network errors, or if they cannot find a healthy primary in the replica sets or sharded cluster.

Write concern describes the level of acknowledgment requested from MongoDB for write operations to a standalone mongod or to replica sets or to sharded clusters. In sharded clusters, mongos instances will pass the write concern on to the shards. ( if your replica set has 3 members that mean that your majority=2 , meaning that your write operation will receive acknowledgment after it receive success confirmation from at least 2 members)

Related