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?
Database.RunCommand()is to facilitate calling MongoDB'srunCommand()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
usersInfocommand which gets you the same data. Its syntax is:This is how you can execute this
usersInfocommand:Note that the
usersInfodocument has various specifications, for example:As you can see, the
getUser()command is a shorthand for{ usersInfo: <username> }which you can call like this:You can of course use an array if you want info about multiple users: