Mongodb – How to query the mongoDB admin database

mongodb

I'm trying to execute the below commands taken from the mongoDB docs

use admin
db.system.version.find( { _id: "authSchema" })

But running those in Robo 3t gives the following output

switched to db admin

Error: error: { 
"$err" : "not authorized for query on admin.system.version", "code" : 13 
}

The user I'm logged in with has the dbOwner role.

Some people suggest granting the root role to the user, but this fails, saying 'not authorized'

db.grantRolesToUser(
    "fooUser",
    [ "readWrite" , { role: "root", db: "admin" } ]
)

It seems like a bad idea to grant root access just for this one query anyways, but I don't see any better solution.

So, how does one query the admin database in mongoDB?

(Current mongoDB version is 2.6.11)

Best Answer

As per your log statement you are getting the error message such as

Error: error: { 
"$err" : "not authorized for query on admin.system.version", "code" : 13 
}

As github mongodb documentation here error_code("Unauthorized", 13).

As per MongoDB documentation here MongoDB grants access to data and commands through role-based authorization and provides built-in roles that provide the different levels of access commonly needed in a database system. You can additionally create user-defined roles.

A role grants privileges to perform sets of actions on defined resources. A given role applies to the database on which it is defined and can grant access down to a collection level of granularity.

You need to assign the read role or readWrite to the user.

For example :

Create Administrative User with Roles

use admin
db.createUser(
   {
     user: "appAdmin",
     pwd: "password",
     roles:
       [
         { role: "readWrite", db: "config" }

       ]
   }
)

For further your ref here, here and here