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?
It feels that this method has been deprecated, I needed the following command to work:
db.getUsers({filter: {'user': 'login'}})
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With