Created
March 10, 2016 00:34
-
-
Save neil-s/abf76e50f7e8588ab7f7 to your computer and use it in GitHub Desktop.
Async writes issue
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
var insertedRecords = 0; | |
var insertRecords = function(db){ | |
fs.createReadStream("./some_file.csv").pipe(converter); | |
converter.on("record_parsed", function (jsonRecord) { | |
try { | |
mongoRecord = db.collection('some_collection') | |
.update( | |
{"some_key": parsedRecord["some_key"]}, | |
parsedRecord, | |
{upsert: true} | |
); | |
} catch (e) { | |
console.error("Error with record: " + jsonRecord.some_key); | |
console.error(e); | |
} | |
insertedRecords += 1; | |
if (insertedRecords % 100 === 0) { | |
console.log("Inserted " + insertedRecords); | |
} | |
}); | |
converter.on("end_parsed", function(jsonObject) { | |
console.log("Finished parsing"); | |
}); | |
}; | |
try { | |
insertRecords(db); | |
} catch (e) { | |
console.error(e); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment