can't shard collection: Uniqueness can't be maintained unless shard key is a prefix
来源:互联网 发布:手机整点报时软件 编辑:程序博客网 时间:2024/06/16 22:10
跟着前一篇搭建MongoDB 3.4分片及副本集集群,接着进行分片,一开始是按照如下方式进行建立唯一索引以及片键(我是先对集合建立唯一索引以及分片索引,再对集合分片)
// 索引 DBObject dbObj = new BasicDBObject(); dbObj.put("notifyTime", 1); dbObj.put("uid", 1); //建立唯一索引 DBObject option=new BasicDBObject(); option.put("unique",true); dbCollection.createIndex(dbObj,option); // 分片索引 dbObj = new BasicDBObject(); dbObj.put("sharedKey", "hashed"); dbCollection.createIndex(dbObj);
结果建立索引后对该集合进行分片,报错:
can't shard collection 'test_collection' with unique index on { notifyTime: 1, uid: 1 } and proposed shard key { sharedKey: "hashed" }. Uniqueness can't be maintained unless shard key is a prefix
报错的意思是:无法对在字段notifyTime
和uid
上建立唯一索引的集合使用sharedKey
字段作为片键对集合进行分片,除非片键是唯一索引的键的前缀,否则唯一性无法保证。
1)不建立唯一索引,即如下所示:
// 索引 DBObject dbObj = new BasicDBObject(); dbObj.put("notifyTime", 1); dbObj.put("uid", 1); dbCollection.createIndex(dbObj); // 分片索引 dbObj = new BasicDBObject(); dbObj.put("sharedKey", "hashed"); dbCollection.createIndex(dbObj);
2)建立唯一索引,将片键作为唯一索引键的前缀,如下所示:
// 索引 DBObject dbObj = new BasicDBObject(); //sharedKey片键要放在唯一组合键开头,即为prefix dbObj.put("sharedKey",1); dbObj.put("notifyTime", 1); dbObj.put("uid", 1); DBObject option=new BasicDBObject(); option.put("unique",true); dbCollection.createIndex(dbObj,option); // 分片索引 dbObj = new BasicDBObject(); dbObj.put("sharedKey", "hashed"); dbCollection.createIndex(dbObj);
阅读全文
0 0
- can't shard collection: Uniqueness can't be maintained unless shard key is a prefix
- Can't access SelectionStart unless the Editor is in edit mode
- delphi-“can't be installed because it is not a design
- Methode ** can't be reflected
- ssh can't be established.
- module rewrite_module is built-in and can't be loaded
- R can't be resolved to a variable解决办法
- Error---Uri can't be resolved to a type
- JSONObject can't be resolved to a type解决办法
- shard
- How to Choose a MongoDB Shard Key
- Can't be better than this...
- Provisioning profile can't be found
- control cleared invoice can't be cancelled
- Provisioning profile can't be found
- Provisioning profile can't be found 2
- Provisioning profile XXXX can't be found
- D - Can't be easier 几何计算
- Git学习之快速入门
- 在CSDN上如何快速转载博客
- 数据库设计原则、表&字段命名规则、索引调优建立规则
- 8月10日集训
- 存储过程与触发器
- can't shard collection: Uniqueness can't be maintained unless shard key is a prefix
- ubuntu 安装jekins
- flume是配置和使用
- 删除小写字母字符串中重复字符。
- jdk版本:运行环境的JDK版本与编辑环境的JDK版本不一致的问题
- LeetCode 633. Sum of Square Number
- 8月10日小结
- hdu---(1848)Fibonacci again and again
- 修改linux文件权限的命令chmod