MongoDB – How to Update Collection with SSH from a Shell Script

mongodb

I am trying to update a mongoDb collection via ssh from a shell script but getting the below error :

MongoDB shell version: 2.6.4
connecting to: localhost:27017/b2b_tempdb
2017-12-05T11:04:51.153+0000 SyntaxError: Unexpected token :

Below is the full command :

ssh -n happy@localhost  " mongo --host localhost:27017  --authenticationDatabase admin b2b_tempdb --eval 'db=db.getSiblingDB( \"b2b_tempdb\" ); db[\"b2b_tempdb.activity.preferences\"].update({},{ $set: {'codeId':\"happy.l2l-09l2l121\"} }, {multi:true})' "

Kindly help me in getting it resolved .
Thanks in advance for the help .

Best Answer

For me this works fine

ssh mongodb.domain.com "mongo --eval='db.getSiblingDB(\"testi\").empty.update({},{\$set:{key:1}},{multi:true})'"

The catch is \ before $ because otherways shell will "expand" that $set "enviroment variable", what of course don't exists.