I learn MongoDB and play around with databases. I often run database servers so I need to change the path to some DB a lot. I made a script for that

--dbpath <path_to_some_folder>/data/db

But I want my script to use a path to itself so I don't have to set <path_to_some_folder> every time I copy the script to a different folder. How to use the path to the script as a part of a path to a db? Like that:

--dbpath <path_to_the_script_folder>/data/db

This is for a Bourne shell script, might need to adjust a little for another shell (and for correct mongo syntax).

dir=`dirname $0`
mongo --dbpath $dir/data/db

Unclear on whether MongoDB can use relative paths but you could try doing this:

--dbpath ./data/db

All that dot (.) means is “relative to the current directory.”

But this will only work if you are launching MongoDB in the same path relative to that data of course. Anything else with you launching MongoDB in one place but the data directories being in another would need a more robust solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.