2

I am building a component that is consumable by NPM. The application needs to connect to the database. If the user does not specify the details to the database uri in the config files, my application should spin up an instance of mongoDB from within the application itself.

I have seen many tutorials on how to integrate mongoDB into node.js, express, mocha and many other libraries, but all connect to an external database that's already running.

mongoDB server is being included as part of the dependencies so is installed with everything else, but I'm coming up short on material online on how to do boot up the database when the application starts.

Any help would be greatly appreciated. I suspect the information is out there, but I'm having a hard time finding it :(

3
  • 1
    A database is logically a seperate application, it's actually nice to seperate your node application and the mongodb application, having them "loosely" coupled. Wouldn't it be an option create a new mongo db server for such a user where no config files are specified? You won't have to implement stuff that is already implemented better by the mongo db application. Commented Nov 7, 2016 at 21:32
  • Yes, I agree. How difficult is it to install and create a new mongodb server and dataset on demand with little to no user input at all? Just a flag in the configs to a custom DB and if it doesn't exist, then create a new server. Thanks Commented Nov 7, 2016 at 23:02
  • Hey Nicholas I formulated an answer based on your requirements in the comments. Good luck! Commented Nov 8, 2016 at 7:41

2 Answers 2

3

You can look into starting a subprocess. So letting Node run mongo as an external program. This way you could create a new mongodb instance for any user that fits that criteria. You can use for example the node "child process" library and use the spawn function to 'spawn' a new database instance by writing there the console command as already stated by Larry Turtis or here mongodb - multiple instances. It could look like this, to start the instance:

const spawn = require('child_process').spawn;
const pipe = spawn('mongod', ['--dbpath=<LOCATION>', '--port', '<PORT>'])

You can pipe console output to node with this:

    pipe.stdout.on('data', function (data) {
        printback(data.toString('utf8'));
    });

    pipe.stderr.on('data', (data) => {
        printback(data.toString('utf8'));
    });

    pipe.on('close', (code) => {
        callback('Process exited with code: '+ code);
    });

And kill the mongodb running instance by keeping the pipe reference and then doing this:

static end_pipe(pipe) {
    pipe.kill('SIGINT');
}
Sign up to request clarification or add additional context in comments.

1 Comment

Having error with this aproach: Error: spawn mongod ENOENT , should mongo be given a specific stdio ? (nodejs.org/api/child_process.html#child_process_options_stdio)
2

The command to boot mongoDB is available on the NPM mongodb page. Adding --fork and --logpath because I assume you want it to start in the background.

mongod --dbpath=/data --port 27017 --fork --logpath /var/log/mongod.log

You could just include this as part of your npm start script in package.json:

"scripts": {"start": "mongod --dbpath=/data --port 27017 --fork --logpath /var/log/mongod.log"}

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.