mongo --shell file.js and "use" statement
mongodb, sublimetext2, sublimetext3
Solution
`use dbname` is a helper function in the interactive shell which does not work when you are using mongo shell with a JS script file like you are.
There are multiple solutions to this. The best one, IMO is to explicitly pass the DB name along with host and port name to mongo like this:
`mongo hostname:27017/dbname` mongoscript.js // replace 27017 with your port number
A better way to do this would be to define the DB at the beginning of your script:
mydb=db.getSiblingDB("yourdbname");
mydb.collection.findOne();
etc.
The latter is preferable as it allows you to interact with multiple DBs in the same script if you need to do so.
Problem
can't find solution for simple question: I have file text.js ``` use somedb db.somecollection.findOne() ``` When I run this file in cmd with redirection command from file: "mongo < text.js" it's work properly But when I try this way "mongo text.js" or "mongo --shell test.js" I got this error message MongoDB shell version: 2.2.0 connecting to: test type "help" for help Wed Dec 05 16:05:21 SyntaxError: missing ; before statement pathToFile\test.js.js:1 failed to load: pathToFile\test.js.js It's fail on "use somedb". If I remove this line, it's run without error, but console is clear. is there any idea, what is this and how to fix? I'm tying to find sollution for this, to create build tool for Sublime Text 2. default build file was ``` { "cmd": ["mongo","$file"] } ``` but in this case I get the error above PS. right after posting this question I find sollution for SublimeText2: ``` { "selector": "source.js", "shell":true, "cmd": ["mongo < ${file}"] } ``` PSS. right after posting this question I find sollution for SublimeText3: ``` { "selector": "source.js", "shell":true, "cmd": ["mongo","<", "$file"] } ``` this build tool work properly