Question

I'm playing around with node.js and streams trying to dump an entire mongoDB collection to a file but it isn't working. I suspect it has something to do with old style streams and 0.10 streams but I could be wrong.

The code below can also be found here: github.com

mongodb-collection-dump is in that repo as well.

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);
});
Était-ce utile?

La solution

You can use mongodump or mongoexport in order to create dumps of mongodb.

mongodump is better for backing up and stores not just data, but a lot of meta data about the collections, like indexes. As well you can call it to specific database (-d) as well as collection (-c).

To do it from node.js, use child_process.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top