User accounts are stored in the db.system.users
collection of each database. The User
object contains _id
, user
, pwd
, roles
, and sometimes otherDBRoles
fields. There are a couple different ways to get a list of user objects. The first is to change the database to the one you want to list users on and then execute the show users
command. The following example changes to the admin
database and lists users as shown in Figure 12.3:
use admin
show users
You can also use a query such as find
on the db.system.users
collection. However, db.system.users.find()
returns a cursor object that you can use to access the User
documents. For example, the following code gets a cursor for users in the admin
database and returns the count of users:
use admin
cur = db.system.users.find()
cur.count()
18.118.139.15