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.

3
  • try using db.'testCollection'.insert({"result":1}) Commented Mar 21, 2018 at 9:51
  • This did not work, gave a different error Commented Mar 21, 2018 at 13:04
  • try this db.getCollection(testCollection).insert({"result":1}) Commented Mar 21, 2018 at 16:34

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

Comments

0

You need to specify the db name as shown below.

mongo your-db-name < output.js

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.