0

I'm running MongoDB 4.0: I'm trying to load a script in Mongo:

var MongoClient = require("mongodb").MongoClient;
var database = 'mongodb://localhost:27017,localhost:27018,localhost:27019/myDb?replicaSet=rs'

MongoClient.connect(database)
.then( db => {
  return db.command("load('myScript.js')");
})
.catch( err => {
  console.log(err);
});

I get an error message saying:

MongoError: no such command: '0'

In the Mongo shell the command load('myScript.js') works fine. How to run it from the Mongo driver or from Mongoose?

João Otero
  • 948
  • 1
  • 15
  • 30
  • try db.runCommand( { } ) – agravat.in Sep 01 '18 at 05:57
  • like this it doesn't work, because it's not a proper json: db.runCommand( { "load('myScript.js')" } ) ...how would it be to express "load" in json syntax? – João Otero Sep 01 '18 at 06:04
  • besides, just changing to db.runCommand("...") it gives an error: "db.runCommand is not a function" – João Otero Sep 01 '18 at 06:06
  • 2
    `load` as well as `db.command` are shell commands. It would not run in your context that way. You may need to look into loading/requiring files before you run your app via the `mongo shell` or `mocha -r` etc. https://stackoverflow.com/questions/4837673/how-to-execute-mongo-commands-through-shell-scripts, https://stackoverflow.com/questions/18654563/running-a-set-of-actions-before-every-test-file-in-mocha – Akrion Sep 01 '18 at 06:41
  • that's the info I needed, thanks Akrion – João Otero Sep 01 '18 at 06:55

0 Answers0