【Mongo】mongosshard唯一索引的问题
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 }
已建立唯一索引的collect 去shard 。分片字段必须是唯一索引的前缀
本文标题:【Mongo】mongosshard唯一索引的问题
网页地址:http://pwwzsj.com/article/ghspoj.html