2

I am trying to update a field from command line (NOT from mongo shell)

mongo mydb --eval "db.users.update({}, { $set : {  email : "[email protected]" } })"

results into

Fri Oct 24 12:23:46.102 JavaScript execution failed: SyntaxError: Unexpected token :

Again trying

mongo mydb --eval "db.users.update({}, { $set : {  email : \"[email protected]\" } })"

same results

Fri Oct 24 12:24:05.559 JavaScript execution failed: SyntaxError: Unexpected token :

Any help for the same ?

1
  • had updated the question entirely. but thanks to Neil Lunn for pointing out. now i have updated this question back to previous one. and asked another one here stackoverflow.com/questions/26543376/… Commented Oct 24, 2014 at 7:32

1 Answer 1

2

This is basically just quoting. The shell is generally more forgiving internally but does expect valid JSON otherwise:

mongo mydb --eval "db.users.update({}, { '$set': {  'email' : '[email protected]' } })"
MongoDB shell version: 2.6.5
connecting to: mydb
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Actually, to play more nicely with other modifiers such a multi then reverse the type of quotes used:

mongo mydb --eval 'db.users.update({}, { "$set": {  "email": "[email protected]" } },{ "multi": true })
Sign up to request clarification or add additional context in comments.

8 Comments

you answer is absolutely perfect. But actually my core requirement is little different for the question i asked previously. please review the question again.
@Koka You should ask a different question rather than change your question. But why use a command process when you could just use the driver to connect to the database and issue an update?
actually i am having 100s of other mongodb operations to perform over approx 10 databases, so i thought using a mongo shell script is better approach.
IMO first question had no special significance as you answered in question 'This is basically just quoting'.. and also my requirement is little different than what i asked in first question. But i will keep in mind not repeat this mistake again.
@Koka No it will not be a better approach and in fact far worse. You should ask another question to explain what you want to do.
|

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.