Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongo javascript --eval is failing

This works within the mongo terminal

$ mongo datas
> db.mydb.update({},{$set : {'ids.my_key':''}}, {multi:true})

But fails when eval'd from the command line.

mongo datas --eval "db.mydb.update({},{$set : {'ids.my_key':''}}, {multi:true})"
MongoDB shell version: 2.4.5
connecting to: datas
Thu Dec 12 12:35:24.063 JavaScript execution failed: SyntaxError: Unexpected token :

I assume it's quote-related, but haven't been able to get it to work.

like image 240
Mittenchops Avatar asked May 25 '26 11:05

Mittenchops


2 Answers

The problem is not mongo shell, it is your OS shell which is trying to interpret some of the characters you are wanting to pass to mongo. Try the same syntax but with single quotes around the entire expression and double quotes inside:

$ mongo datas --eval "db.mydb.update({},{$set : {'ids.my_key':''}}, {multi:true})"
MongoDB shell version: 2.4.5
connecting to: datas
Thu Dec 12 18:14:22.049 JavaScript execution failed: SyntaxError: Unexpected token :
$ mongo datas --eval 'db.mydb.update({},{$set : {"ids.my_key":""}}, {multi:true})'
MongoDB shell version: 2.4.5
connecting to: datas
$ 
like image 108
Asya Kamsky Avatar answered May 28 '26 01:05

Asya Kamsky


Try to update mongo shell version: script executed successfully on 2.4.8.

like image 34
FLCL Avatar answered May 28 '26 01:05

FLCL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!