简体   繁体   中英

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.

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
$ 

尝试更新mongo shell版本:脚本在2.4.8上成功执行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM