mongoexport与参数+ node.js +子进程 [英] mongoexport with parameters + node.js + child process

查看:225
本文介绍了mongoexport与参数+ node.js +子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用node.js从mongodb导出csv。为此,我开始与这段代码:

I am trying to export a csv using node.js from mongodb. For this i started with this code:

app.get('/export', function(req, res) {
 var spawn = require('child_process').spawn,
 ls = spawn('mongoexport');
res.sendfile('/home/database.csv'); 
});

这很好。然后为了使它更可用我试图用mongoexport代码下面使用参数:

And this works fine. Then for making it more usable i tried to code below with mongoexport using arguments:

 app.get('/export', function(req, res) {
 var spawn = require('child_process').spawn,
 ls = spawn('mongoexport --db lms --collection databases --fields firstname,lastname,email,daytimePhone,addressOne,city,state,postalCode,areaOfStudy,currentEducationLevel,company --csv --out /home/database.csv');
res.sendfile('/home/database.csv') 

});

这会抛出异常:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: spawn ENOENT
    at errnoException (child_process.js:980:11)
    at Process.ChildProcess._handle.onexit (child_process.js:771:34)

然后我试图只使用一个参数,但它给出相同的错误

Then i tried using only one argument but it gives the same error :(

我也试过这个也看到参数是否工作方式,但同样的错误:

I tried this also to see if arguments work this way but the same error:

spawn('mongoexport',['--csv']);


推荐答案

spawn的语法是:

The syntax for spawn is:

spawn(<command>, [array of arguments]);

例如,执行 ls 命令与 -l / home 选项将如下所示:

For example, doing a ls command with -l /home options would look like this:

ls = spawn('ls', ['-l', '/home'];

您的 spawn('mongoexport',[' - csv']); 标题方向正确,但 mongoexport --csv 无效,这就是为什么你得到错误。 mongoexport需要的不仅仅是 - csv 。像你上面做的,你需要指定数据库名称( -dlms),集合名称( -c databases),字段名称( - 字段firstname,lastname )等。

So your spawn('mongoexport',['--csv']); is heading in the right direction but mongoexport --csv is not valid. That's why you're getting error. mongoexport needs more than just --csv. Like what you've done above, you, for instance, need to specify database name (-d "lms"), collection name (-c "databases"), field names (--fields firstname,lastname), and etc.

在你的case,它应该是这样的:

In your case, it should be something like this:

 var spawn = require('child_process').spawn;
 app.get('/export', function(req, res) {
     var mongoExport = spawn('mongoexport', [ 
         '--db', 'lms', '--collection', 'databases', 
         '--fields',
         'firstname,lastname,email,daytimePhone,addressOne,city,state,postalCode,areaOfStudy,currentEducationLevel,company',   
         '--csv'
     ]);

     res.set('Content-Type', 'text/plain');
     mongoExport.stdout.on('data', function (data) {
         if (data) {
             // You can change or add something else here to the
             // reponse if you like before returning it.  Count
             // number of entries returned by mongoexport for example
             res.send(data.toString());
         } else {
             res.send('mongoexport returns no data');
         }
     });
}

这篇关于mongoexport与参数+ node.js +子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆