通过shell脚本执行mongo命令的问题

时间:2018-03-21 09:39:55

标签: mongodb

根据Matt对here提出的问题的回答:

我尝试了以下脚本

use sample

db.testCollection.insert({"result":1})

然而,当我按照建议运行时,会出现如下错误,并且记录会插入到默认数据库的集合中,而不会插入到“'示例”中。数据库如预期的那样。

[tmp]# mongo < output.js
connecting to: mongodb://127.0.0.1:27017
2018-03-21T14:54:52.713+0530 E QUERY    [thread1] SyntaxError: unterminated string literal @(shellhelp2):1:21
WriteResult({ "nInserted" : 1 })
bye

请求帮助。

2 个答案:

答案 0 :(得分:2)

您的脚本和命令正确。
在Unix上,如果脚本的Dos / Windows行尾(CRLF)而不是Unix行尾(LF),则会出现此错误。

要转换行尾:

dos2unix output.js

或:How to convert DOS/Windows newline (CRLF) to Unix newline (LF) in a Bash script?

答案 1 :(得分:0)

您需要指定数据库名称,如下所示。

mongo your-db-name < output.js
相关问题