Last active
May 24, 2022 18:46
-
-
Save dikey0ficial/9e42822743cc15a969acf46977efc0fe to your computer and use it in GitHub Desktop.
Javascript/Node.js/mongosh shell function to dump collections to .json file
This file contains 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
// 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