0

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?

1 Answer 1

1

Based on the mongodb docs, you are able to select another database with:

mongo mongodb://root:ROOT_PASSWORD@HOST:PORT/NEW_DB_NAME --eval "${CMD}"

This syntax is the only way to connect to a specific database. (https://docs.mongodb.com/manual/reference/program/mongo/#cmdoption-mongo-arg-db)

Otherwise you will get a file not found error because it will try to execute a JavaScript file. (https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/#execute-a-javascript-file)

Besides the connection string, why do you want to connect to another database when the information in the roles options points to the 'admin' database?

roles: [{ role: "dbAdmin", db: "admin" }, "readWrite"]

What do you think of using 'db.getSiblingDB("NEW_DB_NAME")' within your JavaScript function, as an alternative?

You can use db.getSiblingDB() as an alternative to the use helper. This is particularly useful when writing scripts using the mongo shell where the use helper is not available. (https://docs.mongodb.com/manual/reference/method/db.getSiblingDB/)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man! db.getSiblingDB came in really handy! As for your question, whether admin or NEW_DB_NAME , the new mongo user will have access to the new db (I tested). I'm not sure what the difference is though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.