Problem Statement
Which statement is TRUE about the primary node in a replica set?
Explanation
The primary node is the only member of a replica set that receives write operations. All writes go to the primary, which then records the operations in its oplog.
Secondary nodes replicate the oplog and apply the same operations to their data sets. The primary can also serve read operations, though you can configure read preferences to distribute reads to secondaries. Only one node can be primary at a time, and it is elected automatically by the replica set members.
Code Solution
SolutionRead Only
// Connect to replica set
mongo "mongodb://mongodb0.example.net,mongodb1.example.net,mongodb2.example.net/?replicaSet=myReplicaSet"
// Write operations go to primary only
db.users.insertOne({ name: "Alice" })
// Check which node is primary
rs.isMaster()