Reference Error when inserting a string with eval into mongodb - mongodb

I would like to insert data into my mongodb from the command line. The mongodb is running in a docker container.
I want to do the following steps:
Enter docker container
Insert data into mongodb
Exit container
The command that I am using is the following:
docker exec -i <id-of-mongo-container> sh -c "mongo --eval 'db.rules.insert({ description: "test123"})'"
But this throws the following exception:
[js] uncaught exception: ReferenceError: test123 is not defined
I think it is an issue with the quote mark mix, but I am not able to figure out how to get it right. Does someone have an idea on how to fix this?
Research
Something I found from research is that if you swap the string "test123" with a number e.g. 2. Than the command does work.
Therefore, the following command does work.
docker exec -i <id-of-mongo-container> sh -c 'mongo admin --eval "db.rules.insert({ description: 2})"'
It also seems to related to this issue, but it is different because there is an extra set of qoutes from the docker exec.
Cheers!

Related

Postgres + Docker: pg_dump does not work from outside

I want to setup a cron script that automatically creates dumps from a specific Postgres database running in a Docker container. I know how to execute commands in a container from outside and also am familiar with pg_dump.
Somehow, for my container and database , I can't seem to make it work:
docker exec <container> pg_dump -U postgres <mydb> > /pg-snaps/<mydb>_$(date).sql
I get the following error:
zsh: no such file or directory: /pg-snaps/<mydb>_<date>.sql
The directory /pg-snaps exists. I can execute the same command inside the container, and it works. I have no idea why it doesn't work with docker exec. I looked up the methodology on how to do this, and it suggests the same as I want to do. Adding " " around the command to be executed also results in a 'no such file or directory'.
try this example:
docker exec -it <container> sh -c 'pg_dump -U postgres <mydb> > /pg-snaps/<mydb>_$(date).sql'

Cannot get time during PostgreSQL backup in Docker container

I am trying to use the following approach in order to backup PostgreSQL database that is in a Docker container:
cmd /c 'docker exec -t postgres-dev pg_dump dvdrental -U postgres -c -v >
C:\\postgresql\\backup\\dvdrental_%date:~-4,4%.%date:~-7,2%.%date:~-10,2%_%time:~0,2%.%time:~3,2%.sql'
However, it gives the following file that has the size ok 0 KB and with the extension of %tme:
dvdrental_2021.05.24_%tme
I tried some different combinations, but it still the same and I am not able to get backup with the following format:
dvdrental_2021.05.24_15.30
So, how can I get the expected result as mentioned above? I use Windows, but I think it is not important as the command is executed on Docker container.

Kubernetes mongo DB user creation

I am trying to create a Mongo db user using the following method via Jenkins Job
kubectl exec -i ${POD} -- sh -c "mongo --eval 'db.createUser({user:"DBusernmae",pwd:"test",roles:[{role:"dbAdmin",db:"training"}]})'"
receiving following error
2021-02-09T10:50:38.641+0000 E QUERY [js] ReferenceError: DBusernmae is not defined :
Please help me on this
You have to use proper quotation: your first " starts with mongo and ends with DBusernmae
You need to escape the quote ":
kubectl exec -i ${POD} -- sh -c \
"mongo \"mongodb://admin:pwd#localhost:27017\" --eval 'db.createUser({user:\"DBusernmae\",pwd:\"test\",roles:[{role:\"dbAdmin\",db:\"training\"}]})'"
I'd be grateful if somebody found a nice way to do this that doesn't require the community or enterprise operators though.

Cannot mongodump to a single archive

I'm trying to dump a MongoDB database to an archive. Using the following command as given in documentation.
sudo mongodump --uri=mongodb://username:password#host:27017/dbname?authMechanism=SCRAM-SHA-1&authSource=authdb --archive=file.archive
But it doesn't dump as expected rather it creates a dump folder with .json file for each collection, which should be a single archive file as given.
It also shows the following error -
--archive=file.archive: command not found
Mongo version -
MongoDB shell version v3.6.3
I had this problem & I figured out the reason it was happening.
The command I was running was
sudo /usr/bin/mongodump --uri=mongodb+srv://{username}:{password}#{atlasendpoint}.mongodb.net/?retryWrites=true&w=majority --archive={filename}.archive --gzip 2>&1
I was getting the same error as you. I change the shell command by wrapping quotation marks around the URL; this fixed it for me.
sudo /usr/bin/mongodump --uri="mongodb+srv://{username}:{password}#{atlasendpoint}.mongodb.net/?retryWrites=true&w=majority" --archive={filename}.archive --gzip 2>&1

How to fill in a Docker mongodb image

I don't really understand how I can fill in my mongodb image with a simple script like demoInstall.js.
I have my image and I can launch a mongodb instance. But I can not access to the docker image "mongo shell" to fill this one with the script.
I tried this command :
sudo docker run --entrypoint=/bin/cat mongo/ubuntu /tmp/devInstall.js | mongo --host IPAdress
But it's using the local mongo and not the image :/
Finally my aim is simple, I need to pull my image on a virgin server and launch a basic bash script who fill some informations in the Docker db.
The command you use does pipe on the output of the docker locally. You might call with explicit bash -c instead:
sudo docker run -it --rm mongo/ubuntu /bin/bash -c '/bin/cat /tmp/devInstall.js | mongo --host IPAdress'
I am not sure the IPAdress will be available though. You might want to define it via environmental parameter or container linking.
I would mount a volume with this argument:
-v /local_init_script_folder:/bootstrap
And then with a similar commandline like you proposed, you cann access the contents of the folder as /bootstrap from within the container.