Created
March 14, 2014 16:20
-
-
Save just-boris/9551081 to your computer and use it in GitHub Desktop.
mongo import and export
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/node | |
"use strict"; | |
var fs = require('fs'), | |
exec = require('child_process').exec; | |
var db = process.argv[2], | |
out_dir = process.argv[3], | |
host = process.argv[4] || 'localhost'; | |
if(!out_dir) { | |
out_dir = './'; | |
} | |
else { | |
if(!fs.statSync(out_dir).isDirectory()) { | |
fs.mkdirSync(out_dir); | |
} | |
} | |
exec('mongo ' + db + ' --host ' + host + ' --quiet --eval "print(db.getCollectionNames())"', function(err, stdout, stderr) { | |
if(err) { | |
console.log(stdout); | |
console.error(stderr); | |
throw new Error(err); | |
} | |
stdout.split(',').forEach(exportCollection); | |
}); | |
function exportCollection(collection) { | |
console.log('Exporting: '+collection); | |
exec('mongoexport --host ' + host + ' -d ' + db + ' -c "' + collection + '" --jsonArray', function(err, stdout) { | |
if(err) { | |
throw new Error(err); | |
} | |
var data = JSON.parse(stdout); | |
console.log(collection+': '+data.length+' records'); | |
fs.writeFile(out_dir+'/'+db+'_'+collection+'.json', JSON.stringify(data, null, " "), function(err) { | |
if(err) { | |
throw new Error(err); | |
} | |
}); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/node | |
"use strict"; | |
var fs = require('fs'), | |
path = require('path'), | |
exec = require('child_process').exec; | |
var db = process.argv[2], | |
src_dir = process.argv[3], | |
host = process.argv[4] || 'localhost'; | |
fs.readdir(src_dir, function(err, files) { | |
if(err) { | |
throw new Error(err); | |
} | |
files = files.filter(function(file) { return path.extname(file) === '.json';}); | |
files.forEach(importCollection); | |
}); | |
function importCollection(file) { | |
var collection = file.substring(file.indexOf('_')+1, file.lastIndexOf('.')); | |
console.log('Import: '+collection); | |
exec('mongoimport --host ' + host + ' -d ' + db + ' -c "' + collection + '" --jsonArray < "'+src_dir+'/'+file+'"', function(err, stdout, stderr) { | |
console.log(stdout); | |
if(err) { | |
console.error(stderr); | |
throw new Error(err); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment