How to change the shard key

我与影子孤独终老i 提交于 2019-12-10 00:45:39

问题


I Know that impossible to change shard key. But, when I set incorrect shard key, How to change that?


回答1:


dump the collection you sharded.. import it again.. set the new shard key.




回答2:


dump collection

mongodump --host <hostname> --port <port> --collection <collection_name> --db <db_name>

open mongos and drop database or collection(If you had more than 1 collection)

mongo --host <hostname> --port <port>
show dbs
use <db_name>
db.dropDatabase() //it's only if you hade ONE database in db
exit

import database

mongorestore --host <hostname> --port <port> --collection <collection_name> --db <db_name> <path to <collection_name>.bson>

open mongos and shard

mongo --host <hostname> --port <port>
sh.status() (only to understand what is happen)
sh.enableSharding("<db_name>")
sh.shardCollection("<db_name>.<collection_name>",<shard key>,<option>)

something like that p.s. You must had index in collection for shard key. Search: "ensureIndex()"




回答3:


Thank you for your process

I'm working with MongoDB 3.0 and :

  1. mongoimport is not the tool to import db dumped with mongodump.
  2. It is mongorestore which work fine with exactly the same arguments

Regards




回答4:


It is very simple, use remove + insert instead of update.

var buf = db.col.findOne({'_id': ObjectId(<id>)});
buf['key'] = 'new key';
db.col.remove({'_id': ObjectId(<id>)});
db.col.insert(buf); //_id does not change!


来源:https://stackoverflow.com/questions/6622635/how-to-change-the-shard-key

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!