0
0条评论

一、知识背景
https://docs.mongodb.com/manual/reference/method/sh.updateZoneKeyRange/#sh.updateZoneKeyRange
Compound Shard Key
Given a sharded collection <span class="pre">exampledb.collection</span> with a shard key of <span class="pre">{</span> <span class="pre">a</span> <span class="pre">:</span> <span class="pre">1,</span> <span class="pre">b</span> <span class="pre">:</span> <span class="pre">1</span> <span class="pre">}</span>, the following operation creates a range covering the lower bound of <span class="pre">{</span> <span class="pre">a:</span> <span class="pre">1,</span> <span class="pre">b</span> <span class="pre">:</span> <span class="pre">1</span> <span class="pre">}</span> and an upper bound of <span class="pre">{</span> <span class="pre">a</span> <span class="pre">:</span> <span class="pre">10,</span> <span class="pre">b</span> <span class="pre">:</span> <span class="pre">10}</span> and associates it with the <span class="pre">alpha</span> zone:

copy
copied

<span class="nx">sh</span><span class="p">.</span><span class="nx">updateZoneKeyRange</span><span class="p">(</span>
   <span class="s2">"exampledb.collection"</span><span class="p">,</span>
   <span class="p">{</span> <span class="nx">a</span> <span class="o">:</span> <span class="mi">1</span><span class="p">,</span> <span class="nx">b</span> <span class="o">:</span> <span class="mi">1</span> <span class="p">},</span>
   <span class="p">{</span> <span class="nx">a</span> <span class="o">:</span> <span class="mi">10</span><span class="p">,</span> <span class="nx">b</span> <span class="o">:</span> <span class="mi">10</span> <span class="p">},</span>
   <span class="s2">"alpha"</span>
<span class="p">)</span>

If you wanted to create a range on values of <span class="pre">b</span> only, you must specify the entire range of <span class="pre">a</span> when creating ranges. For example, the following operations create two ranges on <span class="pre">b</span> and associates them to the <span class="pre">beta</span> zone.

NOTE

The previously defined range conflicts with the ranges defined in this example. Issue the <span class="pre">sh.removeRangeFromZone()</span> operation to remove an existing conflicting range.

copy
copied

<span class="nx">sh</span><span class="p">.</span><span class="nx">updateZoneKeyRange</span><span class="p">(</span>
   <span class="s2">"exampledb.collection"</span><span class="p">,</span>
   <span class="p">{</span> <span class="nx">a</span> <span class="o">:</span> <span class="nx">MinKey</span><span class="p">,</span> <span class="nx">b</span> <span class="o">:</span> <span class="mi">1</span> <span class="p">},</span>
   <span class="p">{</span> <span class="nx">a</span> <span class="o">:</span> <span class="nx">MaxKey</span><span class="p">,</span> <span class="nx">b</span><span class="o">:</span> <span class="mi">10</span> <span class="p">},</span>
   <span class="s2">"alpha"</span>
<span class="p">);</span>

<span class="nx">sh</span><span class="p">.</span><span class="nx">updateZoneKeyRange</span><span class="p">(</span>
   <span class="s2">"exampledb.collection"</span><span class="p">,</span>
   <span class="p">{</span> <span class="nx">a</span> <span class="o">:</span> <span class="nx">MinKey</span><span class="p">,</span> <span class="nx">b</span> <span class="o">:</span> <span class="mi">10</span> <span class="p">},</span>
   <span class="p">{</span> <span class="nx">a</span> <span class="o">:</span> <span class="nx">MaxKey</span><span class="p">,</span> <span class="nx">b</span><span class="o">:</span> <span class="mi">20</span> <span class="p">},</span>
   <span class="s2">"beta"</span>
<span class="p">);</span>

二、目的:
自定义一个集合,片键为:{ “playerGuid” : 1, “zoneId” : 1},我想值针对zoneId这个字段进行划分zone,前缀索引playerGuid忽略
三、操作步骤如下:
1.集合分片
mongos> sh.shardCollection(
…   “shard-test.player”,
…   { “playerGuid” : 1, “zoneId” : 1}
… )
{
“collectionsharded” : “shard-test.player”,
“collectionUUID” : UUID(“53d2f653-cded-465a-8271-30cd44d5e2ad”),
“ok” : 1,
“operationTime” : Timestamp(1612331428, 5),
“$clusterTime” : {
“clusterTime” : Timestamp(1612331428, 5),
“signature” : {
“hash” : BinData(0,”AAAAAAAAAAAAAAAAAAAAAAAAAAA=”),
“keyId” : NumberLong(0)
}
}
}
2.分配第一个zone
mongos> sh.updateZoneKeyRange(“shard-test.player”, { playerGuid: MinKey,zoneId: NumberLong(1)}, { playerGuid: MaxKey, zoneId:NumberLong(10) }, “zone0”)
{
“ok” : 1,
“operationTime” : Timestamp(1612331435, 1),
“$clusterTime” : {
“clusterTime” : Timestamp(1612331435, 1),
“signature” : {
“hash” : BinData(0,”AAAAAAAAAAAAAAAAAAAAAAAAAAA=”),
“keyId” : NumberLong(0)
}

3.在分配第二个zone时报错
mongos> sh.updateZoneKeyRange(“shard-test.player”, { playerGuid: MinKey,zoneId: NumberLong(10)}, { playerGuid: MaxKey, zoneId:NumberLong(20) }, “zone1”)
{
“ok” : 0,
“errmsg” : “Zone range: { playerGuid: MinKey, zoneId: 10 } –>> { playerGuid: MaxKey, zoneId: 20 }  on  zone1 is overlapping with existing: { playerGuid: MinKey, zoneId: 1 } –>> { playerGuid: MaxKey, zoneId: 10 }  on  zone0”,
“code” : 178,
“codeName” : “RangeOverlapConflict”,
“operationTime” : Timestamp(1612331487, 2),
“$clusterTime” : {
“clusterTime” : Timestamp(1612331487, 2),
“signature” : {
“hash” : BinData(0,”AAAAAAAAAAAAAAAAAAAAAAAAAAA=”),
“keyId” : NumberLong(0)
}
}
}
麻烦大路大神给看下是用错了吗?

更改状态以发布