Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-9845

Command to unshard a collection

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.4.3
    • Component/s: Sharding
    • None

      I think these commands can be used to unshard a collection with moving all chunks to primary of database.

      Unable to find source-code formatter for language: javacsript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      database = 'test'
      collection = database + '.fs.chunks'
      
      sh.stopBalancer()
      use config
      primary = db.databases.findOne({_id: database}).primary
      
      // move all chunks to primary
      db.chunks.find({ns: collection, shard: {$ne: primary}}).forEach(function(chunk){
      	print('moving chunk from', chunk.shard, 'to', primary, '::', tojson(chunk.min), '-->', tojson(chunk.max));
      	sh.moveChunk(collection, chunk.min, primary);
      });
      
      // unshard
      db.collections.remove({ "_id" : "test.fs.chunks" })
      db.chunks.remove({ ns : collection })
      
      // flush all mongos, is this neccessary?!
      use admin
      db.runCommand({ flushRouterConfig: 1 })
      

      Is this a correct code? Did I missed something or not?

            Assignee:
            steffan.mejia@mongodb.com Steffan Mejia
            Reporter:
            taha_jahangir Taha Jahangir
            Votes:
            0 Vote for this issue
            Watchers:
            11 Start watching this issue

              Created:
              Updated:
              Resolved: