Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB仅将数据存储到主分片,以防选择标记分片键_Mongodb_Sharding_Database_Nosql - Fatal编程技术网

MongoDB仅将数据存储到主分片,以防选择标记分片键

MongoDB仅将数据存储到主分片,以防选择标记分片键,mongodb,sharding,database,nosql,Mongodb,Sharding,Database,Nosql,我正在使用mongodb v3.0,并尝试使用标记碎片进行切分。 文档说明了如何配置它 我有两个碎片: mongos> sh.addShardTag("shard0000", "USA") mongos> sh.addShardTag("shard0001", "EU") 切分的集合是测试集合: mongos> db.printShardingStatus() --- Sharding Status --- sharding version: { "_id" : 1,

我正在使用mongodb v3.0,并尝试使用标记碎片进行切分。 文档说明了如何配置它

我有两个碎片:

mongos> sh.addShardTag("shard0000", "USA")
mongos> sh.addShardTag("shard0001", "EU")
切分的集合是测试集合:

mongos> db.printShardingStatus()
--- Sharding Status --- 
  sharding version: {
 "_id" : 1,
 "version" : 3,
 "minCompatibleVersion" : 3,
 "currentVersion" : 4,
 "clusterId" : ObjectId("555df7f4f6506e6ba07e1f20")
}
  shards:
 {  "_id" : "shard0000",  "host" : "127.0.0.1:27017",  "tags" : [  "USA" ] }
 {  "_id" : "shard0001",  "host" : "127.0.0.1:27018",  "tags" : [  "EU" ] }
  databases:
 {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
 {  "_id" : "test_collection",  "partitioned" : true,  "primary" : "shard0000" }
正如您在本例中所看到的,它选择shard0000作为主要对象。 当我标记了集合并启用了用于切分的集合时:

mongos> sh.addTagRange("test_collection.items", {country: "USA"}, {country: "USA"}, "USA")
mongos> sh.addTagRange("test_collection.items", {country: "EU"}, {country: "EU"}, "EU")
mongos> db.runCommand({shardCollection: "test_collection.items", key: {"_id": "hashed"}})
和插入的数据(大约20K项),我检查了状态

mongos> db.printShardingStatus()
--- Sharding Status --- 
  sharding version: {
 "_id" : 1,
 "version" : 3,
 "minCompatibleVersion" : 3,
 "currentVersion" : 4,
 "clusterId" : ObjectId("555df7f4f6506e6ba07e1f20")
}
  shards:
 {  "_id" : "shard0000",  "host" : "127.0.0.1:27017",  "tags" : [  "USA" ] }
 {  "_id" : "shard0001",  "host" : "127.0.0.1:27018",  "tags" : [  "EU" ] }
  databases:
 {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
 {  "_id" : "test_collection",  "partitioned" : true,  "primary" : "shard0000" }
  test_collection.items
   shard key: { "_id" : "hashed" }
   chunks:
    shard0000 1
   { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0) 
    tag: EU  { "country" : "EU" } -->> { "country" : "EU" }
    tag: USA  { "country" : "USA" } -->> { "country" : "USA" }
因此,应用了碎片标记,但数据只写入主碎片

我按照手册所做的每一步,有人能告诉我配置中遗漏了什么吗?
相同的行为仅在选择碎片标记时显示。没有标签,例如,仅通过分片键,它工作正常,数据存储到两个分片

问题是您的shard键是散列的“\u id”列,但您在“country”列上标记范围

根据文件,这是不可能的:

MongoDB支持标记一系列切分键

您需要将“国家”列作为切分键的一部分。

谢谢,Borut

我试过用“国家”作为切分键,但也没用。也许我在配置上又犯了一些错误。但在上面的评论之后,我重复了所有的步骤,并使切分配置正常工作

因此,我在下面一步一步地发布所有流程以获得配置:

  • 在用标签标记碎片(已添加碎片)后,检查状态
分片版本:{
“_id”:1,
“版本”:3,
"小外翻":3,,
“当前版本”:4,
“clusterId”:ObjectId(“555df7f4f6506e6ba07e1f20”)
}碎片:{
“_id”:“shard0000”,
“主机”:“127.0.0.1:27000”,
“标签”:[
“美国”
]
}{
“_id”:“shard001”,
“主机”:“127.0.0.1:270001”,
“标签”:[
“欧盟”
]
}数据库:{
“_id”:“管理员”,
“分割”:假,
“主”:“配置”
}{
“_id”:“test_db”,
“分割”:正确,
“主”:“shard0000”
}
  • 然后将碎片密钥添加到我们将使用的集合中
db.runCommand({shardCollection:“test_db.items”,键:{“country”:1}})

注意,在本例中,我选择了尽可能简单的键,但在实际系统中,您应该更仔细地选择它

为我们的碎片添加标记范围:
sh.addTagRange(“test_db.items”,{country:“MX”},{country:“USA”},USA”)
sh.addTagRange(“test_db.items”,{country:ES},{country:GER},EU”)

分片版本:{
“_id”:1,
“最小相容外翻”:4,
“当前版本”:5,
“clusterId”:ObjectId(“556376557b13cb3ac8b90b49”)
}
碎片:
{“_id”:“shard0000”,“host”:“127.0.0.1:27000”,“tags”:[“USA”]
}
{“_id”:“shard001”,“host”:“127.0.0.1:27001”,“tags”:[“EU”]}
数据库:
{“_id”:“admin”,“partitioned”:false,“primary”:“config”}
{“\u id”:“test\u db”,“partitioned”:true,“primary”:“shard0000”}
测试项目
切分键:{“国家”:1}
块:
Shard0002
shard0001 1
{“国家”:{“$minKey”:1}}-->{“国家”:“ES”
}on:shard0000时间戳(2,1)
{“国家”:“ES”}-->{“国家”:“MX”}on:shar
d0001时间戳(2,0)
{“国家”:“MX”}-->{“国家”:{“$maxKey”:1
}}on:shard0000时间戳(1,2)
标签:EU{“国家”:“ES”}-->{“国家”:“GER”
}
标签:美国{“国家”:“MX”}-->{“国家”:“美国”
}
  • 好的,让我们向集合中添加元素
(变量i=0;i)的