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);
});
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.