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

Command to unshard a collection

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Duplicate
    • Affects Version/s: 2.4.3
    • Fix Version/s: None
    • Component/s: Sharding
    • Labels:
      None

      Description

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

      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?

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                9 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: