英文:
Create role for mongo
问题
在Go语言中,我正在向MongoDB添加一个用户,并希望创建一个角色,该角色只能访问特定的集合。我对MongoDB还不太熟悉,所以不太确定如何实现这一点。我尝试使用RunCommand方法,以下是我尝试的代码,但是我得到了一个运行时错误:invalid memory address or nil pointer dereference。
下面是我的代码,我已经验证了数据库和id的值已经正确设置:
createRoleResult := client.Database(db).RunCommand(ctx, bson.D{{"createRole", id},
{"privileges", []bson.M{
{
"resource": bson.A{bson.M{"db": db}, bson.M{"collection": id}, bson.M{"actions": bson.A{"update", "insert", "remove"}}},
},
}}})
这段代码导致了相同的错误:
createRoleResult := client.Database(db).RunCommand(ctx, bson.D{{"createRole", id},
{"privileges", []bson.A{
{
bson.D{bson.E{"resource", bson.A{bson.D{bson.E{"db", db}}, bson.D{bson.E{"collection", id}}, bson.E{"actions", bson.A{"update", "insert", "remove"}}}}},
},
}}})
如果对此有任何建议,将不胜感激!如果有更好的方法,我愿意倾听。
以下是可行的Shell代码:
db.createRole(
{
role: "ABC",
privileges: [
{ resource: { db: "MyDB", collection: "ABC" }, actions: [ "update", "insert", "remove", "find" ] }
],
roles: []
}
)
谢谢!
英文:
In Go, I am adding a user to mongo and want to create a role, which grants access only to a specific collection. I am new to mongo, so not sure exactly how to do this. My attempt is to use the RunCommand. Below is what I tried, but I am getting a:
runtime error: invalid memory address or nil pointer dereference
Below my code, I verified that the values for database and id are properly set.
createRoleResult := client.Database(db).RunCommand(ctx, bson.D{{"createRole", id},
{"privileges", []bson.M{
{
"resource": bson.A{bson.M{"db": db}, bson.M{"collection": id}, bson.M{"actions": bson.A{"update", "insert", "remove"}}},
},
}}})
This code results in the same:
createRoleResult := client.Database(db).RunCommand(ctx, bson.D{{"createRole", id},
{"privileges", []bson.A{
{
bson.D{bson.E{"resource", bson.A{bson.D{bson.E{"db", db}}, bson.D{bson.E{"collection", id}}, bson.E{"actions", bson.A{"update", "insert", "remove"}}}}},
},
}}})
Any advice would be appreciated! If there are better approaches to this, I'm all ears.
Working shell code:
db.createRole(
{
role: "ABC",
privileges: [
{ resource: { db: "MyDB", collection: "ABC" }, actions: [ "update", "insert", "remove", "find" ] }
],
roles: []
}
)
Thanks!
答案1
得分: 0
你可以尝试这段 Golang 代码:
var commandResult bson.M
command := bson.D{{"createRole", "ABC"}, {"privileges", bson.A{bson.D{{"resource", bson.D{{"db", "MyDB"}, {"collection", "ABC"}}}, {"actions", bson.A{"update", "insert"}}}}}, {"roles", bson.A{}}}
err = client.Database("test").RunCommand(context.TODO(), command).Decode(&commandResult)
if err != nil {
log.Fatal(err)
}
fmt.Println(commandResult)
请注意,这段代码使用了 Golang 的 bson 包,它用于处理 BSON 数据。
英文:
You can try this Golang code:
var commandResult bson.M
command := bson.D{{ "createRole", "ABC" }, { "privileges", bson.A{ bson.D{{ "resource", bson.D{{ "db", "MyDB" }, { "collection", "ABC" }}}, { "actions", bson.A{ "update", "insert" }}}}}, { "roles", bson.A{}}}
err = client.Database("test").RunCommand(context.TODO(), command).Decode(&commandResult)
if err != nil {
log.Fatal(err)
}
fmt.Println(commandResult)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论