Update Record in Replica Set

I’m currently working on a replica set MongoDB. The current replica set up is:

  1. Pc0 (primary node)
  2. Pc2 (secondary node)
  3. Pc1 (seconard node)

I need to update one value in a collection within a replica set db. I’m just wondering the best way to do this? Do I have to connect directly to the primary node and update in there? Will that automatically update the secondary nodes? Should I switch off the secondary’s whilst updating the primary node? I’m using MongoDB version 4.0

Any help would be greatly appreciated :slight_smile:

Hi @Flouncy_Mgoo

I’m not sure I understand the question. What do you mean exactly by “update one value in a collection”? Could you give an example of what you mean?

A MongoDB replica set provides you with high availability and an application connects to the replica set as a whole. With MongoDB, no matter the topology of your deployment (standalone, replica set, or a sharded cluster), there would be no difference in database commands from the application side. The only difference is the connection string URI you use.

Please see Replication for more details into how a replica set works in MongoDB.

Also MongoDB 4.0 series was out of support since April 2022 and will not receive further updates and fixes. You might want to move to a supported version (the 4.2 series is the oldest still-supported series) as soon as possible.

Best regards
Kevin

Hi Kevin, apologies I wasn’t very clear. I basically just need to replace one single NULL value to a string in a table. I’m just worried about the change not being reflected in all of the replica sets if I edit the NULL value using the wrong connection URI (ie the connection string containing the all members of the set rather than just the primary node)

Let me know if this does not clear things up

Thanks

Hi @Flouncy_Mgoo

I’m just worried about the change not being reflected in all of the replica sets if I edit the NULL value using the wrong connection URI

Modern official MongoDB drivers will by default try to discover the topology you’re connecting to, and once a replica set is setup properly, it will propagate any writes to all nodes in the set, since this is done server-side. In a MongoDB replica set, only the primary can accept writes, and the secondaries replicate those writes.

I’m curious about the origin of your question though. Are you developing an app using MongoDB, or are you administering it? Did you inherit this setup from someone else, or did you set it up yourself?

Best regards
Kevin

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.