简体   繁体   English

mongo javascript --eval失败了

[英]mongo javascript --eval is failing

This works within the mongo terminal 这适用于mongo终端

$ 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. 问题不在于mongo shell,而是你的OS shell试图解释你想要传递给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上成功执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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