2

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

mongo mydb --eval "db.users.update({}, { $set : {  email : "email@email.com" } })"

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@email.com\" } })"

same results

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

Any help for the same ?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
codeofnode
  • 18,169
  • 29
  • 85
  • 142
  • 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 http://stackoverflow.com/questions/26543376/how-to-pass-command-string-in-nodejs-childprocess-exec-function-to-update-a-stri – codeofnode Oct 24 '14 at 07:32

1 Answers1

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@email.com' } })"
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@email.com" } },{ "multi": true })
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • you answer is absolutely perfect. But actually my core requirement is little different for the question i asked previously. please review the question again. – codeofnode Oct 24 '14 at 07:14
  • @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? – Neil Lunn Oct 24 '14 at 07:17
  • 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. – codeofnode Oct 24 '14 at 07:20
  • 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. – codeofnode Oct 24 '14 at 07:23
  • @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. – Neil Lunn Oct 24 '14 at 07:23
  • Yes i gonna ask another question. But just for little guidance from your side, what to do with this question.. wont there be another duplicate, if i ask another one. – codeofnode Oct 24 '14 at 07:25
  • @Koka as for your opinion. It is extremely rude to ask "what colour is the sky?" and then come back after you have been answered and say "I mean on Jupiter" which is basically what you have done here. These are two separate questions. – Neil Lunn Oct 24 '14 at 07:26
  • thanks for pointing out .. i have updated this question back to previous one. and asked another one here http://stackoverflow.com/questions/26543376/how-to-pass-command-string-in-nodejs-childprocess-exec-function-to-update-a-stri – codeofnode Oct 24 '14 at 07:31