【Mongo】mongos shard 唯一索引的问题

1.已经被sharding的文档建立唯一索引

  1. mongos> db.testmongo.ensureIndex({'age':1,'name':1},{"unique":1})
  2. {
  3.         "raw" : {
  4.                 "shard1/172.31.32.223:27001,172.31.35.47:27001,172.31.37.105:27001" : {
  5.                         "createdCollectionAutomatically" : false,
  6.                         "numIndexesBefore" : 1,
  7.                         "ok" : 0,
  8.                         "errmsg" : "cannot create unique index over { age: 1.0, name: 1.0 } with shard key pattern { _id: 1.0 }",
  9.                         "code" : 67,
  10.                         "codeName" : "CannotCreateIndex",
  11.                         "$gleStats" : {
  12.                                 "lastOpTime" : {
  13.                                         "ts" : Timestamp(1529656402, 42),
  14.                                         "t" : NumberLong(6)
  15.                                 },
  16.                                 "electionId" : ObjectId("7fffffff0000000000000006")
  17.                         }
  18.                 },
  19.                 "shard2/172.31.32.223:27002,172.31.35.47:27002,172.31.37.105:27002" : {
  20.                         "createdCollectionAutomatically" : true,
  21.                         "numIndexesBefore" : 1,
  22.                         "numIndexesAfter" : 2,
  23.                         "ok" : 1,
  24.                         "$gleStats" : {
  25.                                 "lastOpTime" : {
  26.                                         "ts" : Timestamp(1529656406, 2),
  27.                                         "t" : NumberLong(7)
  28.                                 },
  29.                                 "electionId" : ObjectId("7fffffff0000000000000007")
  30.                         }
  31.                 },
  32.                 "shard3/172.31.32.223:27003,172.31.35.47:27003,172.31.37.105:27003" : {
  33.                         "createdCollectionAutomatically" : true,
  34.                         "numIndexesBefore" : 1,
  35.                         "numIndexesAfter" : 2,
  36.                         "ok" : 1,
  37.                         "$gleStats" : {
  38.                                 "lastOpTime" : {
  39.                                         "ts" : Timestamp(1529656406, 2),
  40.                                         "t" : NumberLong(6)
  41.                                 },
  42.                                 "electionId" : ObjectId("7fffffff0000000000000006")
  43.                         }
  44.                 }
  45.         },
  46.         "code" : 67,
  47.         "ok" : 0,
  48.         "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 }\" }"
  49. }
  50. mongos> db.testmongo.ensureIndex({'_id':1,'age':1,'name':1},{"unique":1})
  51. {
  52.         "raw" : {
  53.                 "shard1/172.31.32.223:27001,172.31.35.47:27001,172.31.37.105:27001" : {
  54.                         "createdCollectionAutomatically" : false,
  55.                         "numIndexesBefore" : 1,
  56.                         "numIndexesAfter" : 2,
  57.                         "ok" : 1,
  58.                         "$gleStats" : {
  59.                                 "lastOpTime" : {
  60.                                         "ts" : Timestamp(1529656437, 1),
  61.                                         "t" : NumberLong(6)
  62.                                 },
  63.                                 "electionId" : ObjectId("7fffffff0000000000000006")
  64.                         }
  65.                 },
  66.                 "shard2/172.31.32.223:27002,172.31.35.47:27002,172.31.37.105:27002" : {
  67.                         "createdCollectionAutomatically" : false,
  68.                         "numIndexesBefore" : 2,
  69.                         "numIndexesAfter" : 3,
  70.                         "ok" : 1,
  71.                         "$gleStats" : {
  72.                                 "lastOpTime" : {
  73.                                         "ts" : Timestamp(1529656437, 1),
  74.                                         "t" : NumberLong(7)
  75.                                 },
  76.                                 "electionId" : ObjectId("7fffffff0000000000000007")
  77.                         }
  78.                 },
  79.                 "shard3/172.31.32.223:27003,172.31.35.47:27003,172.31.37.105:27003" : {
  80.                         "createdCollectionAutomatically" : false,
  81.                         "numIndexesBefore" : 2,
  82.                         "numIndexesAfter" : 3,
  83.                         "ok" : 1,
  84.                         "$gleStats" : {
  85.                                 "lastOpTime" : {
  86.                                         "ts" : Timestamp(1529656437, 1),
  87.                                         "t" : NumberLong(6)
  88.                                 },
  89.                                 "electionId" : ObjectId("7fffffff0000000000000006")
  90.                         }
  91.                 }
  92.         },
  93.         "ok" : 1
  94. }

已经被shard的collection 唯一索引的前缀必须是分片健


2.已经建立唯一索引的collection去shard

  1. 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"
            }
    ]

  2. mongos> sh.shardCollection("welike_mongo.testtt",{_id:1})
  3. {
  4.         "ok" : 0,
  5.         "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"
  6. }
  7. mongos> sh.shardCollection("welike_mongo.testtt",{'name':1})
  8. {
  9.         "ok" : 0,
  10.         "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"
  11. }
  12. mongos> sh.shardCollection("welike_mongo.testtt",{'age':1})
  13. { "collectionsharded" : "welike_mongo.testtt", "ok" : 1 }
已建立唯一索引的collect 去shard 。分片字段必须是唯一索引的前缀


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29096438/viewspace-2156524/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29096438/viewspace-2156524/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值