在Go中使用MongoDB的shardCollection命令。

huangapple go评论92阅读模式
英文:

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)
}

huangapple
  • 本文由 发表于 2016年3月17日 00:56:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/36042161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定