I tried to run a JS file directly from the Mongo shell by using the query
mongo localhost:27017/west createSumCollection.js
However I keep getting the error
"uncaught exception: SyntaxError: unexpected token: identifier : @(shell):1:6"
The contents of CreateSumCollection.js are a simple use database and db.createCollection with a simple object id and another field.
Any idea what I am missing here? If I run the same script in the mongoshell directly it works perfectly.
I tried load("C:/mongodb/data/db/createSumCollection.js") and it failed because of the use west statement inside the .js file. So after removing that, the load worked. My question is what is the difference between load and mongo localhost:27017/west createSumCollection.js and how do i get the js to be run directly.
use is a helper that is only available in the interactive shell.
In the interactive shell (and via load) you might run:
use my_database
db.collection.find()
The equivalent in a javascript file on the command line would be:
db.getSiblingDB("my_database").collection.find()