Skip to content

Instantly share code, notes, and snippets.

@dvideby0
Last active December 14, 2015 13:49
Show Gist options
  • Save dvideby0/5096351 to your computer and use it in GitHub Desktop.
Save dvideby0/5096351 to your computer and use it in GitHub Desktop.
Reading directory of files and importing into mongo
var fs = require('fs');
var file = __dirname + '/test.json';
var Mongolian = require("mongolian");
var server = new Mongolian;
var db = server.db("db");
var collection = db.collection("collection");
fs.readdir('/path/to/html/files', function(err, files) {
files.filter(function(file) { return file.substr(-5) == '.json'); })
.forEach(function(file) {
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
console.log('Error: ' + err);
return;
}
collection.insert(JSON.parse(data), function(){
console.log('Done!');
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment