Last active
December 14, 2015 13:49
-
-
Save dvideby0/5096351 to your computer and use it in GitHub Desktop.
Reading directory of files and importing into mongo
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
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