在我的 mongodb 设置中,我有一个复合分片键{"region" : 1, "foo" : 1, "bar" : 1}
,我知道值区域可以是,并且每个区域应该在一个块上。
因此,我只想根据区域键进行预拆分。之后的分片状态应如下所示:
{ "region": { "$minKey" : 1 }, "foo": { "$minKey" : 1 }, "bar": { "$minKey" : 1 } } -> { "region": region1, "foo": { "$minKey" : 1 }, "bar": { "$minKey" : 1 } } on: shard1
{ "region": region1, "foo": { "$minKey" : 1 }, "bar": { "$minKey" : 1 } } -> { "region": region2, "foo": { "$minKey" : 1 }, "bar": { "$minKey" : 1 } } on: shard2
{ "region": region2, "foo": { "$minKey" : 1 }, "bar": { "$minKey" : 1 } } -> { "region": { "$maxKey" : 1 }, "foo": { "$minKey" : 1 }, "bar": { "$minKey" : 1 } } on: shard3
Run Code Online (Sandbox Code Playgroud)
我尝试了一些方法来实现这一目标,但没有任何效果:
db.runCommand( { split : "mydb.mycollection" , middle : { "region" : "region1" } } );
将返回错误,因为完整的分片键必须是拆分的一部分。db.runCommand( { split : "mydb.mycollection" , middle : { "region" : "region1", "foo" : { "$minKey" : 1 }, "bar" : { "$minKey" : 1 } } } );
并将db.runCommand( { split : "mydb.mycollection" , middle : { "region" : "region1", "foo" : "$minKey" , "bar" : "$minKey" } } );
minKey 解释为 String 并在错误的基础上拆分。我怎样才能最终在单个字段基础上使用复合分片键拆分块?!
干杯。
只是如何传递$minKey
值的一个小问题,请尝试以下操作:
db.adminCommand( { split : "mydb.mycollection" , middle : { "region" : "region1", "foo" : MinKey , "bar" : MinKey } } );
db.adminCommand( { split : "mydb.mycollection" , middle : { "region" : "region2", "foo" : MinKey , "bar" : MinKey } } );
Run Code Online (Sandbox Code Playgroud)
这让我得到了以下布局:
sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 4,
"minCompatibleVersion" : 4,
"currentVersion" : 5,
"clusterId" : ObjectId("53a2cd9d98b4ace818666544")
}
shards:
{ "_id" : "shard0000", "host" : "localhost:30000" }
{ "_id" : "shard0001", "host" : "localhost:30001" }
{ "_id" : "shard0002", "host" : "localhost:30002" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "mydb", "partitioned" : true, "primary" : "shard0001" }
mydb.mycollection
shard key: { "region" : 1, "foo" : 1, "bar" : 1 }
chunks:
shard0000 1
shard0001 2
{
"region" : { "$minKey" : 1 },
"foo" : { "$minKey" : 1 },
"bar" : { "$minKey" : 1 }
} -->> {
"region" : "region1",
"foo" : { "$minKey" : 1 },
"bar" : { "$minKey" : 1 }
} on : shard0000 Timestamp(2, 0)
{
"region" : "region1",
"foo" : { "$minKey" : 1 },
"bar" : { "$minKey" : 1 }
} -->> {
"region" : "region2",
"foo" : { "$minKey" : 1 },
"bar" : { "$minKey" : 1 }
} on : shard0001 Timestamp(2, 2)
{
"region" : "region2",
"foo" : { "$minKey" : 1 },
"bar" : { "$minKey" : 1 }
} -->> {
"region" : { "$maxKey" : 1 },
"foo" : { "$maxKey" : 1 },
"bar" : { "$maxKey" : 1 }
} on : shard0001 Timestamp(2, 3)
Run Code Online (Sandbox Code Playgroud)
$minKey
(MinKey) 和(MaxKey) 值的使用$maxKey
有点难以梳理(除了内部之外,它们很少使用),但文档中有一个不错的说明性示例。