如何配置MongoDB副本集以仅复制指定集合或数据库?
Hey there! Great question—MongoDB's default setup replicates every database and collection in a replica set, but you can absolutely lock this down to only sync specific databases or collections using partial replication. Let's walk through exactly how to do this, step by step.
- Make sure you already have a basic replica set up (minimum 3 nodes: 1 primary, 2 secondaries/arbiter). All nodes need to run the same MongoDB version to avoid compatibility issues.
- Critical note: Nodes configured for partial replication cannot become primary nodes. They have to stay as secondaries, so we'll set their priority/vote count to 0 later to prevent election attempts.
This method lets a secondary node sync only the databases you explicitly list.
Step-by-Step:
Stop the target secondary node's MongoDB service:
sudo systemctl stop mongod(Use your custom stop command if you're not using systemd.)
Edit the node's config file (usually
/etc/mongod.conf). In thereplicationsection, add theonlyparameter to list your target databases:replication: replSetName: "myReplicaSet" # Match your existing replica set name only: ["inventory", "sales"] # List databases you want to replicate; single DB uses ["inventory"]Heads up: The
onlyflag only works on secondary nodes—never set this on your primary.Warning: Wipe the node's existing data directory (old full-replica data will conflict with filtered replication):
sudo rm -rf /var/lib/mongodb/* # Replace with your actual data path—double-check this!Start the MongoDB service back up:
sudo systemctl start mongodRe-add the node to the replica set (with non-electable settings):
Connect to your primary node viamongosh:mongosh --host <primary-ip>:27017Then run these commands:
// Remove the old entry for the secondary rs.remove("<secondary-ip>:27017") // Re-add it with priority/vote set to 0 (non-electable) rs.add({ _id: 1, // Match the node's original _id in rs.conf() host: "<secondary-ip>:27017", priority: 0, votes: 0 }) // Verify the updated config rs.conf()The secondary will now only sync the databases you listed.
For finer control (syncing individual collections within a database), use the filter parameter with a namespace match.
Step-by-Step:
Stop the target secondary node's MongoDB service (same as before):
sudo systemctl stop mongodEdit the config file to add the
filterrule. For example, to sync onlyinventory.productsandsales.orders:replication: replSetName: "myReplicaSet" filter: '{ "ns": { "$in": ["inventory.products", "sales.orders"] } }'nsstands for "namespace" (format:database.collection)- Use
$into list multiple collections you want to sync
Wipe the node's data directory (same warning as before):
sudo rm -rf /var/lib/mongodb/*Start the service and re-add the node to the replica set with non-electable settings—follow the same steps as the database-level filtering section.
Connect to the filtered secondary via mongosh and run these checks:
// Check replication status rs.status() // List databases—only your target ones should appear show dbs // Check collections in the target DB use inventory show collections // Confirm data matches the primary db.products.find().count()
If the counts match the primary's collections, you're good to go!
- Partial replication nodes can never be primary—always set
priority: 0andvotes: 0to avoid cluster issues. - Once you set
onlyorfilter, you can't switch back to full replication without wiping the node's data and reconfiguring. - You can set up multiple secondaries with different filter rules if you need different subsets of data.
- Keep all nodes on the same MongoDB version—partial replication has strict compatibility requirements.
内容的提问来源于stack exchange,提问作者mohamed hamada

