Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB command line to show if a user exists (for puppet 'unless' clause)

Tags:

mongodb

puppet

We are using MongoDB user based authentication, and I want to quickly run a command to check whether a user has already been created in the database, in order that puppet won't repeatedly attempt to create the user.

Here is how we check if the replica set has initialised:

/usr/bin/mongo --host ${members[0]} --quiet --eval 'rs.status().ok' | grep -q 1

Is a similar trick possible with authentication? I've checked the documentation here http://www.mongodb.org/display/DOCS/dbshell+%28mongo%29+Reference and I can't see a way of doing it?

like image 843
leonigmig Avatar asked Oct 12 '12 09:10

leonigmig


3 Answers

It feels that this method has been deprecated, I needed the following command to work:

db.getUsers({filter: {'user': 'login'}})
like image 196
Jean Avatar answered Oct 20 '22 01:10

Jean


Yes, on a given DB, you can use db.system.users.find({user:'login'}).count() which will return 0 if the user does not exist.

like image 43
Stephane Godbillon Avatar answered Oct 20 '22 00:10

Stephane Godbillon


Today I just tried -u and -p options for mongo command and it worked for me:

mongo --port 27037 --quiet -u superuser -p pwd 
    --eval "db.system.users.find({user:'user3'}).count()" admin

Note the last "admin" arg - it is the name of database, to which you are authenticating.

like image 45
infografnet Avatar answered Oct 20 '22 00:10

infografnet