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

发布时间:2020-08-09 20:35:41 作者:小亮520cl
来源:ITPUB博客 阅读:214

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 。分片字段必须是唯一索引的前缀


推荐阅读:
  1. mongo复制集+分片实现海量数据存储
  2. Mongo之架构部署(Replica Sets+Sharding)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mongo 问题 mongos

上一篇:OJVM+GI PSU补丁升级

下一篇:tab滑动特效

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》