For example, I have now a project that works in production and I want to migrate from standalone to Replica Set. The database is already quite large. What is the correct: should I first set up a Replica Set with empty databases - and then use mongodump to transfer data? Or should I first, when tuning the Replica Set on each node, create a database with the same data and then initialize the Replica Set?
Or I want to change the topology of the existing Replica Set and add one or several nodes? How can I transfer data to new nodes? Is it necessary to stop all nodes for this?
Great question. To convert from standalone to replica set, the preferred procedure is to:
Shut down the standalone mongod instance.
Change its config to include the option replication.replSetName, with the name of your new replica set.
Connect to the instance, and run the command rs.initiate(). This will initiate the replica set, and your standalone will now be the primary node.
Start some other mongod instances (at least two).
Add the other instances to your replica set with rs.add(). (You must run this command from the primary node.)
To your second question, if your database is sufficiently large, you should use mongodump or mongoexport to export the data from your primary, and then send the data to your secondary nodes. Then you can import them to your secondary nodes with mongorestore or mongoimport, depending on whether you used mongodump or mongoexport.
You can find more detail on the docs. I hope this helps!