Skip to content

Instantly share code, notes, and snippets.

@dikey0ficial
Last active May 24, 2022 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dikey0ficial/9e42822743cc15a969acf46977efc0fe to your computer and use it in GitHub Desktop.
Save dikey0ficial/9e42822743cc15a969acf46977efc0fe to your computer and use it in GitHub Desktop.
Javascript/Node.js/mongosh shell function to dump collections to .json file
// let db = connect('...')
let fs = require('fs')
function dumpCollection(col_name, dump_path) {
let col = db[col_name].find().toArray();
if (!dump_path) {
dump_path = './backup/'
}
fs.writeFile(dump_path + col_name + '.json', JSON.stringify(col), function (err) {
if (err) {
console.log("Error: " + err);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment