- You do NOT need to have MongoDB "installed" on your machine.
- Leverage docker images and persist your data via docker 'volumes'.
Create a mongo docker instance (from an image) and start a new shell (this is not a MongoDB shell .. just a normal BASH shell)
- NOTE: check the volumn settings for your OS. This is a windows example, which connects the files in the current directly, to a specific folder inside the docker image.
docker run --rm -it -v ${PWD}:/src/scripts --name mongo-cli --network docker_default mongo /bin/bash
network
your existing docker images are running under. This could be defined in your docker-compose yml file
if you're using docker compose.
Now that we're interacting/inside this instance, run a mongo script command:
(this runs the mongo
command which then executes a script under the mongo cli)
mongo --host mongodb.data --port 27017 --username root --password example file-to-execute-with-mongo-commands.js
or against a real server using a 'URI' connection string:
mongo "<uri>" <files.js>
NOTE: yes, the <uri>
needs to be inside 2x quotes " "
. And then the .js
files at the end need to end with .js
.
Run this when you are inside/interacting with your mongodb cli instance:
host = db.serverStatus().host;
prompt = function() { return "db:[" + db + " @ " + host + "] >> "; }
ref: https://docs.mongodb.com/manual/reference/connection-string/
"mongo": {
"connectionString": "mongodb://username:password@localhost:27017"
},