I'm trying to add a user to the mongodb, I have a shell script, but I'm trying to do this using python.
I have the code :
addadmusr="""{ createUser 'admin', pwd 'lint1234', roles ['userAdminAnyDatabase'] }"""
call('/opt/mongodb/bin/mongo -port primport --eval """db.getSiblingDB("""admin""").runCommand(addadmusr)"""',shell = True)
I'm trying to use subprocess calls to get this done, and I have a primport extracted by using the code:
primport = subprocess.check_output('/opt/mongodb/bin/mongo localhost27017 --eval "printjson(rs.isMaster())" | grep "primary"| cut -f1 -d, | cut -f3 -d : | grep -Eo """[0-9]{1,}"""' , shell = True)
when I'm trying to add the user, it says invalid port number "primport" and returns connection failed. My guess is the primport value that Im extracting is not parsing into the call statement that I'm using. I need to use the value extracted (primport) and add the user -admin to the db. Any python alternative also helps.