英文:
MongoDB shardCollection command in Go
问题
我正在尝试在Go中使用mgo库对集合进行分片。看起来我无法弄清楚如何使用Session.Run调用运行命令。以下是我的代码:
if err := session.DB("admin").Run(bson.D{{"shardCollection", "visits.visits-2016-05"}, {"uuid", "1"}}, &result); err != nil {
panic(err)
} else {
fmt.Println(result)
}
我尝试了几种不同的传递键参数的变体,但我始终收到“no shard key”错误。请问我在这里做错了什么?
英文:
I'm trying to shard a collection using mgo library in Go.
Looks like I can't figure out to run commands using Session.Run call.
Here is my code:
if err := session.DB("admin").Run(bson.D{{"shardCollection", "visits.visits-2016-05"}, {"uuid", "1"}}, &result); err != nil {
panic(err)
} else {
fmt.Println(result)
}
I've tried several variants of passing key parameter, but I'm always getting no shard key
error
What am I doing wrong here?
答案1
得分: 2
我认为你遇到的问题是你需要指定第二个字段为key
的键,值为{uuid: 1}
的子文档。这样你就可以匹配Mongo文档中列出的确切字段:https://docs.mongodb.org/manual/reference/command/shardCollection/。
我用于相同过程的代码是:
if err := adminDb.Run(
bson.D{
{"shardCollection", "logs.log_"+dateString},
{"key", bson.M{"sk": "hashed"}},
}, &result); err != nil {
log.Println("Failed to shardCollection logs.log_"+dateString, err)
}
所以你可以尝试:
if err := session.DB("admin").Run(bson.D{{"shardCollection", "visits.visits-2016-05"}, {"key", bson.M{"uuid", "1"}}}, &result); err != nil {
panic(err)
} else {
fmt.Println(result)
}
英文:
I think the issue you're having is that you have to specify that the second field is the key of key
and the value is the subdocument of {uuid: 1}
. This way you're matching the exact fields listed by the mongo documentation: https://docs.mongodb.org/manual/reference/command/shardCollection/.
The code I'm using for the same process is:
if err := adminDb.Run(
bson.D{
{
"shardCollection",
"logs.log_"+dateString,
},
{
"key",
bson.M{
"sk": "hashed",
},
},
}, &result); err != nil {
log.Println("Failed to shardCollection logs.log_"+dateString, err)
}
So you might want to try
if err := session.DB("admin").Run(bson.D{{"shardCollection", "visits.visits-2016-05"}, {"key", bson.M{"uuid", "1"}}}, &result); err != nil {
panic(err)
} else {
fmt.Println(result)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论