1

Based on the answer by Matt for the question asked here:

I tried the below script

use sample

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

However when I run as suggested, get an error as below and the record gets inserted in a collection of the default db and not in the 'sample' db as expected.

[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

Kindly request for help.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
Mahesh S
  • 159
  • 2
  • 13

2 Answers2

8

Your script and command are correct.
On Unix, you will get this error if your script has Dos/Windows end of lines (CRLF) instead of Unix end of lines (LF).

To convert your end of lines :

dos2unix output.js

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

Chris H.
  • 611
  • 6
  • 7
0

You need to specify the db name as shown below.

mongo your-db-name < output.js