Command to unshard a collection

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Duplicate
    • Priority: Major - P3
    • None
    • Affects Version/s: 2.4.3
    • Component/s: Sharding
    • None
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None
    • 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
              Reporter:
              Taha Jahangir
              Votes:
              0 Vote for this issue
              Watchers:
              11 Start watching this issue

                Created:
                Updated:
                Resolved: