0

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!

2
  • 2
    Did you try to use escape quotes sh -c "mongo --eval 'db.rules.insert({ description: \"test123\"})'" Commented Jul 17, 2020 at 10:37
  • @GKS Thank you so much, this pointed me in the right direction! Since I have to run it in an npm script I have to do some crazy escape character magic but it works. This is the tricky part of the npm script, ` 'db.rules.insert({ description: \\\"test123\\\", date: new Date()})'\"`. Thanks again! Commented Jul 17, 2020 at 11:08

0

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.