简体   繁体   English

node.js使用Streams将mongoDB集合写入文件?

[英]node.js write mongoDB collection to file using Streams?

I'm playing around with node.js and streams trying to dump an entire mongoDB collection to a file but it isn't working. 我在玩弄node.js和流,试图将整个mongoDB集合转储到文件中,但是它不起作用。 I suspect it has something to do with old style streams and 0.10 streams but I could be wrong. 我怀疑这与旧样式的流和0.10流有关,但我可能是错的。

The code below can also be found here: github.com 下面的代码也可以在这里找到: github.com

mongodb-collection-dump is in that repo as well. mongodb-collection-dump也位于该存储库中。

var fs = require('fs');
var dump = require('mongodb-collection-dump');

var collectionDumpFile = '/tmp/collection-dump.json';

var f = fs.createWriteStream(collectionDumpFile);

f.on('open', function() {
  var d = dump('mongodb://127.0.0.1/test_db', 'testcollection', f);
});
d.on('end', function(){
  console.log("done in write");
});

d.on('error', function(err){
  console.log("there was an error");
  console.log(err);
});

You can use mongodump or mongoexport in order to create dumps of mongodb. 您可以使用mongodumpmongoexport来创建mongodb的转储。

mongodump is better for backing up and stores not just data, but a lot of meta data about the collections, like indexes. mongodump可以备份并存储数据,还可以存储有关集合的大量元数据(例如索引),因此更好。 As well you can call it to specific database (-d) as well as collection (-c). 您也可以将其称为特定数据库(-d)和集合(-c)。

To do it from node.js, use child_process . 要从node.js进行操作,请使用child_process

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM