How do I execute the mongodump command programmatically from node.js?

后端 未结 2 1004

What is the best way to execute mongodb admin/console commands programmatically from node.js? Specifically, I would like to export a mongodb collection using mongodump after

相关标签:
2条回答
  • 2020-12-28 08:56

    Try using child_process.spawn(command, args):

    var spawn = require('child_process').spawn;
    
    // ...
      collection.insert(docs, {safe:true}, function(err, result) {
        var args = ['--db', 'mydb', '--collection', 'test']
          , mongodump = spawn('/usr/local/bin/mongodump', args);
        mongodump.stdout.on('data', function (data) {
          console.log('stdout: ' + data);
        });
        mongodump.stderr.on('data', function (data) {
          console.log('stderr: ' + data);
        });
        mongodump.on('exit', function (code) {
          console.log('mongodump exited with code ' + code);
        });
      });
    // ...
    
    0 讨论(0)
  • 2020-12-28 09:03

    A different year, a different answer.

    You can use something like Shelljs to exec mongodump or any other commands that the UNIX shell provides.

    0 讨论(0)
提交回复
热议问题