In this recipe, we will look at how to migrate non-sharded data to another shard.
- Connect to the mongos service and inspect the shard status:
use myShardedDB sh.status() --- Sharding Status --- <-- output truncated --> databases: { "_id" : "myShardedDB", "primary" : "shard0002", "partitioned" : true } <-- output truncated -->
- Insert a document in the new (non-sharded) collection of the sharded database:
db.my_col.insert({foo: 'bar'})
- Confirm that the document was stored on the primary shard by fetching the document:
db.my_col.find({foo: 'bar'}).explain()['queryPlanner']['winningPlan'] { "stage" : "SINGLE_SHARD", "shards" : [ { "shardName" : "shard0002", "<--output truncated-->
- Switch the primary to a different shard:
use admin mongos> db.runCommand( { movePrimary: 'myShardedDB', to: 'shard0000' ...