Skip to content

Instantly share code, notes, and snippets.

@raphaellondner-mongodb
Last active March 31, 2018 00:12
Show Gist options
  • Select an option

  • Save raphaellondner-mongodb/b0431429aa8a36a6e36371cbda37f879 to your computer and use it in GitHub Desktop.

Select an option

Save raphaellondner-mongodb/b0431429aa8a36a6e36371cbda37f879 to your computer and use it in GitHub Desktop.
function processEvent(event, context, callback) {
console.log('Calling MongoDB Atlas from AWS Lambda with event: ' + JSON.stringify(event));
var jsonContents = JSON.parse(JSON.stringify(event));
//date conversion for grades array
if(jsonContents.grades != null) {
for(var i = 0, len=jsonContents.grades.length; i < len; i++) {
//use the following line if you want to preserve the original dates
//jsonContents.grades[i].date = new Date(jsonContents.grades[i].date);
//the following line assigns the current date so we can more easily differentiate between similar records
jsonContents.grades[i].date = new Date();
}
}
try
{
MongoClient.connect(atlas_connection_uri, function(err, db)
{
createDoc(db, jsonContents);
});
}
catch(err)
{
console.error('an error occurred', err);
}
}
function createDoc (db, json) {
db.collection('restaurants').insertOne( json, function(err, result) {
if(err!=null) {
console.error("an error occurred in createDoc", err);
}
else {
console.log("Kudos! You just created an entry into the restaurants collection with id: " + result.insertedId);
}
db.close();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment