【Mongo】mongos shard 唯一索引的问题
发表于:2025-11-06 作者:千家信息网编辑
千家信息网最后更新 2025年11月06日,1.已经被sharding的文档建立唯一索引mongos> db.testmongo.ensureIndex({'age':1,'name':1},{"unique":1}){"raw" : {"sh
千家信息网最后更新 2025年11月06日【Mongo】mongos shard 唯一索引的问题
已经被shard的collection 唯一索引的前缀必须是分片健
1.已经被sharding的文档建立唯一索引
- mongos> db.testmongo.ensureIndex({'age':1,'name':1},{"unique":1})
- {
- "raw" : {
- "shard1/172.31.32.223:27001,172.31.35.47:27001,172.31.37.105:27001" : {
- "createdCollectionAutomatically" : false,
- "numIndexesBefore" : 1,
- "ok" : 0,
- "errmsg" : "cannot create unique index over { age: 1.0, name: 1.0 } with shard key pattern { _id: 1.0 }",
- "code" : 67,
- "codeName" : "CannotCreateIndex",
- "$gleStats" : {
- "lastOpTime" : {
- "ts" : Timestamp(1529656402, 42),
- "t" : NumberLong(6)
- },
- "electionId" : ObjectId("7fffffff0000000000000006")
- }
- },
- "shard2/172.31.32.223:27002,172.31.35.47:27002,172.31.37.105:27002" : {
- "createdCollectionAutomatically" : true,
- "numIndexesBefore" : 1,
- "numIndexesAfter" : 2,
- "ok" : 1,
- "$gleStats" : {
- "lastOpTime" : {
- "ts" : Timestamp(1529656406, 2),
- "t" : NumberLong(7)
- },
- "electionId" : ObjectId("7fffffff0000000000000007")
- }
- },
- "shard3/172.31.32.223:27003,172.31.35.47:27003,172.31.37.105:27003" : {
- "createdCollectionAutomatically" : true,
- "numIndexesBefore" : 1,
- "numIndexesAfter" : 2,
- "ok" : 1,
- "$gleStats" : {
- "lastOpTime" : {
- "ts" : Timestamp(1529656406, 2),
- "t" : NumberLong(6)
- },
- "electionId" : ObjectId("7fffffff0000000000000006")
- }
- }
- },
- "code" : 67,
- "ok" : 0,
- "errmsg" : "{ shard1/172.31.32.223:27001,172.31.35.47:27001,172.31.37.105:27001: \"cannot create unique index over { age: 1.0, name: 1.0 } with shard key pattern { _id: 1.0 }\" }"
- }
- mongos> db.testmongo.ensureIndex({'_id':1,'age':1,'name':1},{"unique":1})
- {
- "raw" : {
- "shard1/172.31.32.223:27001,172.31.35.47:27001,172.31.37.105:27001" : {
- "createdCollectionAutomatically" : false,
- "numIndexesBefore" : 1,
- "numIndexesAfter" : 2,
- "ok" : 1,
- "$gleStats" : {
- "lastOpTime" : {
- "ts" : Timestamp(1529656437, 1),
- "t" : NumberLong(6)
- },
- "electionId" : ObjectId("7fffffff0000000000000006")
- }
- },
- "shard2/172.31.32.223:27002,172.31.35.47:27002,172.31.37.105:27002" : {
- "createdCollectionAutomatically" : false,
- "numIndexesBefore" : 2,
- "numIndexesAfter" : 3,
- "ok" : 1,
- "$gleStats" : {
- "lastOpTime" : {
- "ts" : Timestamp(1529656437, 1),
- "t" : NumberLong(7)
- },
- "electionId" : ObjectId("7fffffff0000000000000007")
- }
- },
- "shard3/172.31.32.223:27003,172.31.35.47:27003,172.31.37.105:27003" : {
- "createdCollectionAutomatically" : false,
- "numIndexesBefore" : 2,
- "numIndexesAfter" : 3,
- "ok" : 1,
- "$gleStats" : {
- "lastOpTime" : {
- "ts" : Timestamp(1529656437, 1),
- "t" : NumberLong(6)
- },
- "electionId" : ObjectId("7fffffff0000000000000006")
- }
- }
- },
- "ok" : 1
- }
已经被shard的collection 唯一索引的前缀必须是分片健
2.已经建立唯一索引的collection去shard
- mongos> db.testtt.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "welike_mongo.testtt"
},
{
"v" : 2,
"unique" : true,
"key" : {
"age" : 1,
"name" : 1
},
"name" : "age_1_name_1",
"ns" : "welike_mongo.testtt"
}
] - mongos> sh.shardCollection("welike_mongo.testtt",{_id:1})
- {
- "ok" : 0,
- "errmsg" : "can't shard collection 'welike_mongo.testtt' with unique index on { age: 1.0, name: 1.0 } and proposed shard key { _id: 1.0 }. Uniqueness can't be maintained unless shard key is a prefix"
- }
- mongos> sh.shardCollection("welike_mongo.testtt",{'name':1})
- {
- "ok" : 0,
- "errmsg" : "can't shard collection 'welike_mongo.testtt' with unique index on { age: 1.0, name: 1.0 } and proposed shard key { name: 1.0 }. Uniqueness can't be maintained unless shard key is a prefix"
- }
- mongos> sh.shardCollection("welike_mongo.testtt",{'age':1})
- { "collectionsharded" : "welike_mongo.testtt", "ok" : 1 }
索引
前缀
字段
文档
问题
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
如何筛选表格最大的数据库
软件开发工程师电信解决方案面经
新加坡的网络安全公司
网络安全宣传手抄报模板
web前端和网络安全
网络安全工程师职业培训
不能还原微赞数据库
关于调整网络安全领导小组
预警系统数据库一般包括要素
网络安全和信息化工作发言稿
dw怎么连接数据库连接
2021官方服务器人口普查
关于网络安全发言稿400字
电子商务数据库技术自考试卷
问道手游和九游服务器
数据库三级结构定义
网络安全实施计划工作方案
联通招聘网络技术岗位
公务员网络安全算啥子岗位
北京天马网络技术有限公司
北京拉钩网络技术
视频监控服务器系统
社区网络安全周宣传总结
数据库和编程语言有关系吗
请列举两类常见的软件开发模型
苹果软件开发地址查哪里
新思路网络技术激活码
校园网络安全专题片
三明物流软件开发
mfc软件开发 外包