2

I would like to remove some documents of mongo database from shell script file by using some query.

I use the following command, but this doesn't work.

mongo dbName --eval "db.collection.remove({"param1":"value"})"

How should I delete the documents from mongo database from shell script by command line ??

2
  • Try this db.collection.remove({"param1":"value"},true) Commented Jan 10, 2014 at 9:04
  • I beleive it is your use of ", the bash script is being confused, try something like "db.collection.remove({param1:'value'})" Commented Jan 10, 2014 at 9:05

1 Answer 1

6

Your command line use both double quotes for shell strings, as well as string delimiters in the string itself. This causes confusion to the shell. You can either escape your " in the string:

mongo dbName --eval "db.collection.remove({\"param1\":\"value\"})"

Or use single quotes, like:

mongo dbName --eval 'db.collection.remove({"param1":"value"})"'

I would suggest you use single quotes around the whole string, instead of using single quotes in the string, as this also means that strings that contain an $ are not interpolated or cause other issues.

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

Comments

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.