创建MongoDB的角色

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

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)

huangapple
  • 本文由 发表于 2022年2月21日 23:53:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/71209302.html
匿名

发表评论

匿名网友

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

确定