Search code examples
node.jsmongodbshellsharding

How to execute a mongoDB shell script via Node.js?


I am working on my class project in which I want to demonstrate the use of mongoDB sharding. I am using mongoDB node.js native driver. I got to know there is no sharding functionality in this driver. So, I have to write shell script to do sharding. So, Is it possible to do this somehow like this:

node myfile.js (executes my shell script and run my code)


Solution

  • Given that you already have a shell script, why not execute that through the Child Process module. Just use the below function to run the script that you have.

    child_process.execFileSync(file[, args][, options])

    Note that the script should have run permissions(use chmod a+x script otherwise)