英文:
How to db.getUser() using go mongo-driver
问题
我想使用Go驱动程序获取数据库中的用户详细信息。
例如,在mongoshell中:
> db.getUser("testuser")
null
我该如何构建一个用于此目的的bson.M或bson.D?
我不想传递额外的参数,只想检索数据库中的用户信息。
var op bson.M
command := bson.D{{"getUser", 1}, {"username", "testuser"}}
err = clientInfo.Database(db).RunCommand(context.TODO(), cmd).Decode(&op)
我尝试了类似上面的代码,但返回以下错误:
(CommandNotFound) no such command: 'getUser'
我在这里漏掉了什么?
英文:
I want to get the user details for a db using go driver.
For ex. in mongoshell
> db.getUser("testuser")
null
How do i construct a bson.M or bson.D for this ?
I don't want to pass additional args just retrieve the userinfo of a db
var op bson.M
command := bson.D{{"getUser", 1}, {"username", "testuser"}}
err = clientInfo.Database(db).RunCommand(context.TODO(), cmd).Decode(&op)
I tried something like above but it was returning the below error:
(CommandNotFound) no such command: 'getUser'
What am I missing here?
答案1
得分: 1
Database.RunCommand()
是为了方便调用 MongoDB 的 runCommand()
函数而设计的,也就是说,它是一个运行指定数据库命令的辅助函数。
也就是说,在 mongo shell 中调用的 getUser()
函数是一个函数,而不是一个命令。
但是有一个 usersInfo
命令可以获取相同的数据。它的语法如下:
db.runCommand(
{
usersInfo: <various>,
showCredentials: <Boolean>,
showCustomData: <Boolean>,
showPrivileges: <Boolean>,
showAuthenticationRestrictions: <Boolean>,
filter: <document>,
comment: <any>
}
)
以下是如何执行 usersInfo
命令的示例代码:
var op bson.M
cmd := bson.D{{Key: "usersInfo", Value: bson.M{
"user": "testuser",
"db": "admin",
}}}
err = clientInfo.Database(db).RunCommand(ctx, cmd).Decode(&op)
请注意,usersInfo
文档有各种规范,例如:
> { usersInfo: 1 }
> 返回运行命令的数据库中的用户信息。
> mongosh
提供了 db.getUsers()
辅助函数来调用此命令。
>
> { usersInfo:
> 返回运行命令的数据库中特定用户的信息。
> mongosh
提供了 db.getUser()
辅助函数来调用此命令。
>
> { usersInfo: { user:
> 返回指定名称和数据库的用户信息。
>
> { usersInfo: [ { user:
> { usersInfo: [
> 返回指定用户的信息。
>
> { forAllDBs: true }
> 返回所有数据库中的用户信息。
如你所见,getUser()
命令是 { usersInfo: <username> }
的简写形式,你可以像这样调用它:
var op bson.M
cmd := bson.D{{Key: "usersInfo", Value: "testuser"}}
err = clientInfo.Database(db).RunCommand(ctx, cmd).Decode(&op)
当然,如果你想获取多个用户的信息,你可以使用数组:
cmd := bson.D{{Key: "usersInfo", Value: []string{"testuser", "anotheruser"}}}
英文:
Database.RunCommand()
is to facilitate calling MongoDB's runCommand()
function, that is, it's a helper to run specified database commands.
That said, the getUser()
function you call in the mongo shell is a function, not a command.
But there's a usersInfo
command which gets you the same data. Its syntax is:
db.runCommand(
{
usersInfo: <various>,
showCredentials: <Boolean>,
showCustomData: <Boolean>,
showPrivileges: <Boolean>,
showAuthenticationRestrictions: <Boolean>,
filter: <document>,
comment: <any>
}
)
This is how you can execute this usersInfo
command:
var op bson.M
cmd := bson.D{{Key: "usersInfo", Value: bson.M{
"user": "testuser",
"db": "admin",
}}}
err = clientInfo.Database(db).RunCommand(ctx, cmd).Decode(&op)
Note that the usersInfo
document has various specifications, for example:
> { usersInfo: 1 }
> Returns information about the users in the database where the command is run.
> mongosh
provides the db.getUsers()
helper for this invocation of the command.
>
> { usersInfo: <username> }
> Return information about the a specific user that exists in the database where the command is run.
> mongosh
provides the db.getUser()
helper for this invocation of the command.
>
> { usersInfo: { user: <name>, db: <db> } }
> Returns information about the user specified by the name and database.
>
> { usersInfo: [ { user: <name>, db: <db> }, ... ] }
> { usersInfo: [ <username1>, ... ] }
> Returns information about the specified users.
>
> { forAllDBs: true }
> Returns information about users in all databases.
As you can see, the getUser()
command is a shorthand for { usersInfo: <username> }
which you can call like this:
var op bson.M
cmd := bson.D{{Key: "usersInfo", Value: "testuser"}}
err = clientInfo.Database(db).RunCommand(ctx, cmd).Decode(&op)
You can of course use an array if you want info about multiple users:
cmd := bson.D{{Key: "usersInfo", Value: []string{"testuser", "anotheruser"}}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论