The MongoDB docs here say: mongo STAR_WARS connects to the STAR_WARS database. I was wondering if it is possible to pass in the name of a non-existing database you would to create in such command. I am specifically trying this command as my db is remote.
CMD=$(cat <<EOF
if (db.getUsers({ usersInfo: "${MONGO_USER}" }) == 0) {
db.createUser({
user: "${MONGO_USER}",
pwd: "${MONGO_PASSWORD}",
roles: [{ role: "dbAdmin", db: "admin" }, "readWrite"],
passwordDigestor : "server"
});
}
EOF
)
mongo ${MONGO_ROOT_DSN} NEW_DB_NAME --eval "${CMD}"
MONGO_ROOT_DSN is of format mongodb://root:ROOT_PASSWORD@HOST:PORT/?authSource=admin
The passed in CMD is there to add an additional user to the database.
With this command, I get the following error:
MongoDB shell version v3.4.4
connecting to: mongodb://root:ROOT_PASSWORD@HOST:PORT/?authSource=admin
MongoDB server version: 3.6.12
WARNING: shell and server versions do not match
2020-01-26T09:10:59.003+0000 E - [main] file [NEW_DB_NAME] doesn't exist
failed to load: NEW_DB_NAME
Is this method possible or do I have to connect the shell and use the use NEW_DB_NAME; command? I believe you can't have the use command in non-interactive (e.g. running a script in a Docker container). Therefore, one needs to be able to pass in the name of the db to be created.
Is the warning causing this issue or is it related to the db address format?
Why is this NEW_DB_NAME interpreted as a file name? A file name for what?