Search code examples
mongodbsharding

Migration data between shards with MongoDB


I have the bellow MongoDB sharding with the following configuration :

  • One front end sever
  • One config sever
  • 2 shards : shard01 and shard02
  • A shard collection : collection01
  • 2 zones/ 2 ranges :
  • zone01 { "num" : 0 } -->> { "num" : 10 }
  • zone02 { "num" : 11 } -->> { "num" : 20 }

Is it possible to migrate Data from zone01 to zone02 ?

I tried to remove range and recreate it again but i got duplicate data in the both shards/zones.


Solution

  • In fact the process of migration was in progress after few time the data moves from shard01 to shard02 progressively, bellow the commands i used for that :

    1- Remove the current ranges

    sh.removeRangeFromZone("db2017.collection01", { num: 0  }, { num: 10 })
    sh.removeRangeFromZone("db2017.collection01", { num: 11 }, { num: 20 })
    

    2- Recreate the new ranges with different zones :

    sh.addTagRange("db2017.collection01", { num: 1 }, { num: 10 }, "zone02")
    sh.addTagRange("db2017.collection01", { num: 11 },{ num: 20 }, "zone01")
    

    Just give sometime to your MongoDB it will migrate automatically your data between shards.